I just have a quick question about Python loop conditions in the while loop. I am looking to take input from the user in the form of (score/max score) eg (13/15). I am looking to put this into a while loop that will continuously ask the user to enter the input in this form until they do it right.
So far, I have this
num1 = 0
while num1 !='?/?':
num1 = raw_input("Please enter your score in the form socre/max(eg. 30/50)")
I know how to check if a condition is true with a single number, such as: while x> 18, or if the condition is a string, such as: while x != 'Start'. I am not sure what to use as the parameters for the condition of 15/20, where these two numbers will be input in this exact form 15/20. Any help would be greatly appreciated.
Thank you
-
How can I see where this question is located. I have used the search feature and cannot find anything like it?Ububtunoob– Ububtunoob2018年04月08日 08:06:40 +00:00Commented Apr 8, 2018 at 8:06
-
Click the blue link Asking the user for input until they give a valid response.timgeb– timgeb2018年04月08日 08:07:49 +00:00Commented Apr 8, 2018 at 8:07
-
I have found other conditions such as single numbers like while x >10 or something. But I cannot find anyway to check if the condition of the input is formatted correctlyUbubtunoob– Ububtunoob2018年04月08日 08:08:52 +00:00Commented Apr 8, 2018 at 8:08
-
Read the blue link Asking the user for input until they give a valid response.timgeb– timgeb2018年04月08日 08:09:18 +00:00Commented Apr 8, 2018 at 8:09
-
I have read this. In that case, the condition is in an easy form (x > 18). So the user need only enter in a number greater than 18. My question is how can the condition be set to read if the user entered in their response as 15/20? I have used while num1 != '?/?': but this does not work. (I use the ? as a placeholder )Ububtunoob– Ububtunoob2018年04月08日 08:14:06 +00:00Commented Apr 8, 2018 at 8:14
3 Answers 3
you can use regular expressions for match like
import re
input_str = input("Please enter your score in the form socre/max(eg. 30/50)")
while re.match(r'[0-9]+/[0-9]+',input_str) == None:
input_str = input("Please enter your score in the form socre/max(eg. 30/50)")
print('matched')
1 Comment
the code :
import re
score = input("enter score")
while re.match(r'([0-1][0-5])/15',score) == None:
score = input("enter score")
this will take only score from 0 to 15 out of 15 if u want to change this u can change this condition
[0-1][0-5])/15
[0-1]=stand for first digit
[0-5]=stands for second digit
/15=stands for outof 15
eg if u want out of 30
[0-3][0-9])/30
Comments
You can do that the following way:
while True:
score_input = input("Please enter your score in the form socre/max(eg. 30/50)")
try:
score, max_score = score_input.split('/')
except ValueError:
continue
if score > max_score:
raise ValueError("Score cannot be greater than max. score")
# Do whatever you wanto to do with score and max_score and remember to break out.
I hope this helps!
Comments
Explore related questions
See similar questions with these tags.