I'm wanting to create a while loop that asks the user if he wants to run the program or not... very basic... I know. I was wanting the prompt to ask the user to input "Y" or "y" to have the program run. Like this:

programStart = str(input("Please press 'y' or 'Y' to start the program"))
while programStart == "Y" or programStart == "y":
    #program runs
    programStart = str(input("Please press 'y' or 'Y' to restart the program"))

But it sends me an error message. why is this sending an error message not recognizing the users input??

ps: this while loop works with integers. (if I have the user press 1 to start the program.)

Recommended Answers

All 2 Replies

Your program works fine in python 3.2, You do not need str in front of the user inputs, if something doesn't work in your code try using 'print' statements, like I have with your code here:

programStart = input("Please press 'y' or 'Y' to start the program")
while programStart == "Y" or programStart == "y":
    programStart = input("Please press 'y' or 'Y' to restart the program")
# Python2 uses raw_input() for strings and input() for numbers
# Python3 uses input() for strings

# ----------------------------------------------
# add these few lines near the start of you code
import sys
# make string input work with Python2 or Python3
if sys.version_info[0] < 3:
    input = raw_input
# ----------------------------------------------

programStart = input("Please enter 'y' to start the program: ").lower()
while programStart == "y":
    #program runs
    programStart = input("Please enter 'y' to restart the program: ").lower()
Be a part of the DaniWeb community

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