Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Yes or No code
#1
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?
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Now I get it! First I need to create a variable and then test it with the if conditional. Thank you so much!
Reply
#6
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")
Reply
#7
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.
Reply
#8
@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.
Reply
#9
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020