You have to use raw_input (input tries to run the input as a python expression and this is not what you want) and fix the indentation problem.
name = "not_aneta"
while name!= "aneta":
name = raw_input("What is my name? ")
if name == "aneta":
print "You guessed my name!"
Answer from Avión on Stack OverflowYou have to use raw_input (input tries to run the input as a python expression and this is not what you want) and fix the indentation problem.
name = "not_aneta"
while name!= "aneta":
name = raw_input("What is my name? ")
if name == "aneta":
print "You guessed my name!"
It seems that your are using 2.x so you need raw_input for strings.
name = "not_aneta"
while name!= "aneta":
name = raw_input("What is my name? ")
also, the if statements is pretty pointless because the program won't continue until the user guesses the correct name. So you could just do:
name = "not_aneta"
while name!= "aneta":
name = raw_input("What is my name? ")
print "You guessed my name!"
Using a while loop to ask the user (Y/N) ?
python while loop user input - Stack Overflow
python - User input and while loop - Stack Overflow
How do you loop until correct input is given?
So I have a program that calculates compound interest, and now I need to add a question at the end of my program that asks the user if they would like to run the program again, if they answer with Y or y the program will restart to the beginning, if they answer “N” or “n” the program is over. And if they answer with anything other than y or n it should repeat the question until the user finally enters y or n
What I have so far:
I have my entire program in a while loop and was able to get it to reset while Var == Y or y. But I can’t get it to repeat the question if the user answers with let’s say “q” or “618” or “L”. Does anyone know how to do something like this, please let me know… thank you
You can use str.isdigit method ,Note that if you are in python 2 you need to use raw_input because isdigit() is a string method :
i='0'
count = 0
while i.isdigit():
i = input("Enter a number: ")
in python 2 :
i='0'
count = 0
while i.isdigit():
i = raw_input("Enter a number: ")
You could ask for a number, and then check if the string entered is a digit using the built in isdigit() method. Currently your code does not ask for a digit to be entered, it just uses 0 automatically. It would not account for a user entering a non-number the very first time.
i = raw_input("Enter a number: ")
while i.isdigit():
i = raw_input("Enter a number: ")
It will not run when 1,2,3 are entered because the while loop excludes those numbers.
Code:
while(choice>3 or choice<1):
1,2, and 3 will skip this block- therefore the code inside the block will not be run, and the program will do nothing.
With code as similar as possible to what you had:
try_count = 0
choice = ""
valid = False
while True:
print("Please enter a number between 1 and 3")
while True:
try_count += 1
choice = input("")
try: #make sure the user has entered a number
choice = int(choice)
if choice >= 1 or choice <= 3: #if the value is outside our range
valid = True
break #then we're done!
except:
continue #if the user didn't enter a number, go around again
if try_count >= 10: #if we've done this ten times exit
try_count = 0
break #exit to the outer loop
if valid: #if we've got a proper value, we're done
break #exit the loop
I'm doing an assignment that requires I make a code that creates random numbers in a loop until I give a specific input. I just don't know how to get the loop to run while an input is requested. PLZ HELP!
EDIT:
To clarify the assignment:
Write a program that enters a number of positive integers.
The program ends when the user gives in a negative number.
a) Before the program ends, the largest number is printed.
b) Before the program ends, the smallest number is printed.
c) Before the program ends, the sum of the even numbers and the sum of the odd numbers are printed.
(I can do the a,b,c part no problem. It's the Program that creates the numbers i have problems with)
I asked my teacher what he meant by "a number of positive integers". He stated that the program should create numbers until the negative number is given.
EDIT 2:
I have concluded that I'm an idiot. It is the user who inputs the numbers. Thank you all for the help and not giving up with my dense skull :) (great community👍)