Python Forum

Full Version: Yes or No code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I know this is simple code, but I am in doubt. I need code that interacts with the user, asking if he wants to continue the algorithm or not. If "Yes (Y)" returns to the beginning, if "No (N)" ends execution of the algorithm. Could someone help me?
What have you tried? We're not big on writing code for people here, but we would be happy to help you fix your code when you run into problems. When you do run into problems, please post your code in Python tags, and clearly explain the problem you are having, including the full text of any errors.
Y=True
print("Do you want to return to the start?")
if Y:
   #This code I already have.
else:
   #This code I do not know how to do.
You want input. The input function prints some text and waits for the user to type in a response. It then returns the response as a string. Something like response = input('Do you want to return to the start? '). The you will need to check the value of response, which is a string, rather than testing Y.
Now I get it! First I need to create a variable and then test it with the if conditional. Thank you so much!
You can do it all in that same line:
while input('Do you want to return to the start? ').lower() == 'y':
	print("Do Something")
print("Moving on")
def return ()
     #.
     #.
     #.
    while (a>b):
           print("This value is wrong.")
           question=input("Do you want to return to the start? Enter Y for Yes or N for No")
           if (question==Y):
           return()
           break
    else:
        print("The value is right")
return()
The code above is an example. The code to go back to the start of the program is working. The problem is now the indentation of the IF function inside the WHILE function for return() and break working.
@doug2019 Please check that your code will be accepted by python before posting. The above code is impossible in python because return is a keyword. It cannot be used as a function name.
don't use return as function name
question==Y should be question == 'Y' - you want to compare with string 'Y', not variable name Y, which is not defined

from a structural point of view - it's not clear what/where start is. using global variables (a, b)should be avoided and in this case their use add to confusion. and as the code is now variables a and b are not defined - it's not clear where they come from