!using python 3!

first of all, i am a rather noobish python coder, so please dont make the answers too hard to understand.

I was trying to get an input from the user with raw_input(), that input had to be the same as a predefined string, i came at:

x = raw_input("yes or no: ")
if x == yes #here i got to the problem,
#how do i check for a hardcoded string from an input?
if x == no #same thing

this didnt work of course, but how to ge it to work?

Recommended Answers

All 12 Replies

I think you need speechmarks aroynd yes

Eg if x == "yes"

if i do that, it looks like this:

x = raw_input("yes of no: ")
if x == "yes"
    print "you did it!"

when i try to run it it says invalid syntax for the(if x ==) "yes"

Do you need a semi colon?

if x == "yes":

i actually forgot the ":" after the yes-_-.
now i have the code working:

x = raw_input("yes or no: ")
if x == "yes":
    print "you did it!"
if x == "no":
    print "try again!"

thanks!

only one more question, if user inputs no and the output is try again, can i get the code to try again?

thanks a lot, with the loops ill be able to help myself from retyping a load of code. i should have had a suspicion that such a function excists. I just tried using break to get out of the loop. It worked, so now if i have another while True function after this is there a way to go from the last one to the first?

like:

while True:

    x = raw_input("yes or no: ")
    if x == "yes":
        print "you did it!"
    if x == "no":
        print "try again!"
while True

    z = raw_input("do you want to do it again? (yes or no): ")
    if z == "yes":
        print "now do it again!"
    #here i need to go back to the start
    if z == "no"
        break

what do I need to insert to get back to the first while loop?

Your code tells me that you are using Python2.

Where is your start?

I can't help you any more. I'm not a python developer and have never written a python program. I am a professional software engineer though and was able to help with your basic issues initially but you now need someone with more python knowledge than me.

Also its good practice once an item has been resolved to mark it solved and if you have any other questions then post them as new threads.

That is python 2 by the way as ZZucker mentioned, in python 3 it is input() instead of raw_input, and print statements require brackets as well anyway,
as Dave stated loop through to ask a user if he wants to try again

I will open a new thread for the next question, as for which python i use, if i execute python3 in the terminal(ubuntu 14.04)it says:

Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.

so i guess it actualy is python3.4.0.
I will play around with for loops trying to make it work that way, thanks a lot

Your code is Python2 code and would give you lots of errors in Python3.

yes, i just realized i had both versoins installed, i have just fixed the code to work for python 3. it works, thanks for noticing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.