0

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

Ububtunoob
  • 103
  • 8
  • How can I see where this question is located. I have used the search feature and cannot find anything like it? – Ububtunoob Apr 08 '18 at 08:06
  • Click the blue link [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response). – timgeb Apr 08 '18 at 08: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 correctly – Ububtunoob Apr 08 '18 at 08:08
  • Read the blue link [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response). – timgeb Apr 08 '18 at 08: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 Apr 08 '18 at 08:14
  • `num != 15 or num != 20`? – timgeb Apr 08 '18 at 10:00
  • The user is entering in the condition in the format 12/15. I do not know what the two numbers will be so I cannot hard code them into the condition. I would like to just say while num1!= string/string. Meaning the user has entered in two numbers (strings) in the format of 15/20. One number then a "/" then another number – Ububtunoob Apr 08 '18 at 10:06
  • `split` the string by `/`, then `try/except` converting the two fields of the result to `int`. It's really very, very similar to the examples in the duplicate question. – timgeb Apr 08 '18 at 10:13

3 Answers3

1

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')
user8190410
  • 1,264
  • 2
  • 8
  • 15
  • Thank you. I dug around a lot last night and ended up figuring this out myself. I appreciate your constructive input though:) – Ububtunoob Apr 09 '18 at 08:06
0

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
Aniket Bote
  • 3,456
  • 3
  • 15
  • 33
0

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!

Sanyam Khurana
  • 1,336
  • 1
  • 14
  • 27