This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 1 year ago.
I'm a beginner in Python. I've tried using the 'while' loop. I need the code to allow the user to reattempt the question after their previously incorrect guess. After the first correct try, it says what I want it to say but after the first wrong try, the correct answer is given as incorrect. How do I solve this and what am I doing wrong?
key = input("Choose a place and see if your guess is correct! ")
if key == ("bed"):
print("Congratulations! You are correct and will now progress to room 2.")
while key != ("bed"):
input("Unfortunately your guess is incorrect. Try again. ") ```
First of all you need to indent the 2nd line. Second of all the loop can't work because you say that the loop should stop when the key is "bed" but you do not change the key. The 4th line should be: key = input("Unfortunately your guess is incorrect. Try again. ")
Of course you need to put your if statement in the while loop.
while(True):
if key == ("bed"):
print("Congratulations! You are correct and will now progress to room 2.")
False
else:
key = input("Unfortunately your guess is incorrect. Try againg.")
maybe try it like "this"?
Related
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 2 years ago.
i'm absolutley garbage at python 3. this may not be a hard question but ive tried for hours to create a solution to this question and cant make a functioning code. i've used while and if statements.
"As the user enters passenger details such as e-ticket number, full name, and destination, the program validates the user input. If an invalid input is entered,the program displays an appropriate error message and asks the user to re-enter invalid values until each entry is correct"
this is very easy
helle.py
while True:
inputdata = input()
if inputdata != "valid":
print("not valid")
else:
print("valid")
break
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 3 years ago.
I'm very new to python and code in general and I'm wondering how i can do something like this:
User enters string, computer checks if string is longer then 10 characters and if it is it asks for the user to enter a new string. This is what i have right now.
usernamelength = (len(username))
if usernamelength > 10:
return
else:
print("Hello, %s. Placeholder text." %username)
Sorry if i'm missing out something obvious, as i said earlier I'm new to this. Any help appreciated :)
This is a good place for a while loop instead of that if statement:
while usernamelength > 10:
# ask for the username again
# usernamelength = len(newUsername)
That way you'll just continue to prompt for the username until you're given something over 10 chars.
I have a login system in which students take quizzes at different difficulties. what i need to do is load the question and answers for the quiz from an external .txt file. can someone help me quickly please as i need to have this done very soon. can the coding be simple aswell and available to use on python 3.4 as i am not very good with python
This is my attempt at the code:
def easyMathsQuiz():
score=0
emquiz=open("easymathsquiz.txt","r")
questionNumber=0
for questionNumber,line in enumerate(emquiz):
print (line)
ans=input("Your answer is: ")
if ans == "1":
score=score+1
questionNumber=questionNumber+1
elif ans=="2":
questionNumber=questionNumber+1
elif ans !="1" or ans !="2":
print("You have entered an invalid character")
easyMathsQuiz()
break
for questionNumber,line in enumerate(emquiz):
print(line)
if ans == "2":
score=score+1
questionNumber=questionNumber+1
elif ans=="1":
questionNumber=questionNumber+1
elif ans !="1" or ans !="2":
print("You have entered an invalid character")
easyMathsQuiz()
easyMathsQuiz()
print (score)
This is whats inside the .txt file:
What is 2+2-1? 1)3 2)4
What is 10+10? 1)30 2)20
What is 3*9? 1)27 2)36
What is 100/5? 1)25 2)20
What is 30-17? 1)23 2)13
My problem is:
Each line number basically represnts the question number. the first line i got to print but im just not sure how to make the next line print and i need the system to allow the user to input and of course it needs to check if their answer is correct. And i also have just absolutely no idea how to write code for it to go to the start of the question when they enter an invalid character rather than make the whole thing restart
Btw i cant seem to get the code to indent properly on here everything is indented correctly on my program
This question already has answers here:
How do I read from stdin?
(25 answers)
Closed 17 days ago.
For example, if the program asks the user to press enter on their keyboard to continue, the program can recognise it & continue. I'm still a student so not really sure, is it possible..?
I think you are looking for input command
You need to use the input statement and assign the input to variables like this:
name = input("What's your name? ")
print("Hello there, " + name + "!")
It's that simple!
Use the input function as well as if statements:
x = input("What's 1+1?")
if x == 2:
print("correct")
else:
print("Wrong")
This question already has answers here:
Getting user input [duplicate]
(5 answers)
Closed 5 years ago.
For a challenge I am tasked with creating a unit converter that can change the units. I chose degrees Celsius to Fahrenheit. I am quite new to Python. My problem is that I ask a question on the code e.g.
print("Enter Value: ")
How do I make it so that the value that a user enters becomes the variable f for Fahrenheit which can then be changed to Celsius so I can do this..
print((f - 32) / 1.8)
Can anyone help and explain it in a way a beginner can understand?
Assuming you're using Python3, what you need is:
temp=input("Temperature please?")
print((int(temp)-32)/1.8)
Also, please look up the docs Jacek linked to so that you understand what's really going on here.
Use input() function:
Input and Output Docs
temp = 0
# while loop
# wait until user set a input
while not temp:
# default type in input is "str"
user_input = input("Enter Value: ")
if user_input.isdigit():
temp = user_input
# we know every char is digit
print (((int(temp)-32)/1.8))