Home > Back-end >  How do i make sure that when ever the user enter a number less than 0 or more than 300 it asks them
How do i make sure that when ever the user enter a number less than 0 or more than 300 it asks them

Time:11-07

How do i make sure that when ever the user enter a number less than 0 or more than 300 it asks them to try again. I have trouble with the code. My code works on the first score but the rest it keeps accepting values greater than 300 and less than 0. How would i be able to fix this issue without changing a big part of my code?

#Kenneth Sodjahin
#November 4th 2022

#defining where low,high,total score start from
#defining where couter starts
Low=0
High=0
Counter=0
Total=0

#while loop to check if the numbers are numbers
#if the user wants to quit they press q
#if the Scores are greater than 300 or less than 0
#or if the user presses q, it prints them the highest score,lowest score, counter,and their total score
while True:
    #the code will try these
    try:
        #asks user to enter their score
        Score1=int(input("Enter your 1st score:"))
        if (Score1 > 300) or (Score1 < 0):
                print("Please enter a Number from 0 and 300 included!")
                continue
            
        Quit=input("Enter q to quit or press ENTER to continue:")
        if Quit=='q':
            ScoreList=[Score1]        
            #highest score
            High = max(ScoreList)
            #lowest score
            Low = min(ScoreList)
            Total=Total Score1
            print("High Score is:",High)
            print("Low Score is:",Low)
            print("Total Score is:",Total)
            print("Counter is 1")
            exit()
            
        Score2=int(input("Enter your 2nd score:"))
        if ((Score1 or Score2)> 300) or ((Score1 or Score2)< 0):
                print("Please enter a Number from 0 and 300 included!")
                continue
        Quit=input("Enter q to quit or press ENTER to continue:")
        if Quit=='q':
            ScoreList=[Score1,Score2]        
            #highest score
            High = max(ScoreList)
            #lowest score
            Low = min(ScoreList)
            Total=Total Score1 Score2
            print("High Score is:",High)
            print("Low Score is:",Low)
            print("Total Score is:",Total)
            print("Counter is 2")
            exit()      
        Score3=int(input("Enter your 3rd score:"))
        if ((Score1 or Score2 or Score3)> 300) or ((Score1 or Score2 or Score3)< 0):
                print("Please enter a Number from 0 and 300 included!")
                continue
        Quit=input("Enter q to quit or press ENTER to continue:")
        if Quit=='q':
            ScoreList=[Score1,Score2,Score3]        
            #highest score
            High = max(ScoreList)
            #lowest score
            Low = min(ScoreList)
            Total=Total Score1 Score2 Score3
            print("High Score is:",High)
            print("Low Score is:",Low)
            print("Total Score is:",Total)
            print("Counter is 3")
            exit()      
        Score4=int(input("Enter your 4th score from:"))
        if ((Score1 or Score2 or Score3 or Score4)> 300) or ((Score1 or Score2 or Score3 or Score4)< 0):
                print("Please enter a Number from 0 and 300 included!")
                continue
        Quit=input("Enter q to quit or press ENTER to continue:")
        if Quit=='q':
            ScoreList=[Score1,Score2,Score3,Score4]        
            #highest score
            High = max(ScoreList)
            #lowest score
            Low = min(ScoreList)
            Total=Total Score1 Score2 Score3 Score4
            print("High Score is:",High)
            print("Low Score is:",Low)
            print("Total Score is:",Total)
            print("Counter is 4")
            exit()      
        Score5=int(input("Enter your 5th score:"))
        if ((Score1 or Score2 or Score3 or Score4 or Score5)> 300) or ((Score1 or Score2 or Score3 or Score4 or Score5)< 0):
                print("Please enter a Number from 0 and 300 included!")
                continue
        Quit=input("Enter q to quit or press ENTER to continue:")
        if Quit=='q':
            ScoreList=[Score1,Score2,Score3,Score4,Score5]        
            #highest score
            High = max(ScoreList)
            #lowest score
            Low = min(ScoreList)
            Total=Total Score1 Score2 Score3 Score4 Score5
            print("High Score is:",High)
            print("Low Score is:",Low)
            print("Total Score is:",Total)
            print("Counter is 5")
            exit()      
        Score6=int(input("Enter your 6th score:"))
        if ((Score1 or Score2 or Score3 or Score4 or Score5 or Score6)> 300) or ((Score1 or Score2 or Score3 or Score4 or Score5 or Score6)< 0):
                print("Please enter a Number from 0 and 300 included!")
                continue
        Quit=input("Enter q to quit or press ENTER to continue:")
        if Quit=='q':
            ScoreList=[Score1,Score2,Score3,Score4,Score5,Score6]        
            #highest score
            High = max(ScoreList)
            #lowest score
            Low = min(ScoreList)
            Total=Total Score1 Score2 Score3 Score4 Score5 Score6
            print("High Score is:",High)
            print("Low Score is:",Low)
            print("Total Score is:",Total)
            print("Counter is 6")
            exit()      
        Score7=int(input("Enter your 7th score:"))
        if ((Score1 or Score2 or Score3 or Score4 or Score5 or Score6 or Score7)> 300) or ((Score1 or Score2 or Score3 or Score4 or Score5 or Score6 or Score7)< 0):
                print("Please enter a Number from 0 and 300 included!")
                continue
        Quit=input("Enter q to quit or press ENTER to continue:")
        if Quit=='q':
            ScoreList=[Score1,Score2,Score3,Score4,Score5,Score6,Score7]        
            #highest score
            High = max(ScoreList)
            #lowest score
            Low = min(ScoreList)
            Total=Total Score1 Score2 Score3 Score4 Score5 Score6 Score7
            print("High Score is:",High)
            print("Low Score is:",Low)
            print("Total Score is:",Total)
            print("Counter is 7")
            exit()      
        Score8=int(input("Enter your 8th score:"))
        if ((Score1 or Score2 or Score3 or Score4 or Score5 or Score6 or Score7 or Score8)> 300) or ((Score1 or Score2 or Score3 or Score4 or Score5 or Score6 or Score7 or Score8)< 0):
                print("Please enter a Number from 0 and 300 included!")
                continue
        Quit=input("Enter q to quit or press ENTER to continue:")
        if Quit=='q':
            ScoreList=[Score1,Score2,Score3,Score4,Score5,Score6,Score7,Score8]        
            #highest score
            High = max(ScoreList)
            #lowest score
            Low = min(ScoreList)
            Total=Total Score1 Score2 Score3 Score4 Score5 Score6 Score7 Score8
            print("High Score is:",High)
            print("Low Score is:",Low)
            print("Total Score is:",Total)
            print("Counter is 8")
            exit()      
        Score9=int(input("Enter your 9th score:"))
        if ((Score1 or Score2 or Score3 or Score4 or Score5 or Score6 or Score7 or Score8 or Score9)> 300) or ((Score1 or Score2 or Score3 or Score4 or Score5 or Score6 or Score7 or Score8 or Score9)< 0):
                print("Please enter a Number from 0 and 300 included!")
                continue
        Quit=input("Enter q to quit or press ENTER to continue:")
        if Quit=='q':
            ScoreList=[Score1,Score2,Score3,Score4,Score5,Score6,Score7,Score8,Score9]        
            #highest score
            High = max(ScoreList)
            #lowest score
            Low = min(ScoreList)
            Total=Total Score1 Score2 Score3 Score4 Score5 Score6 Score7 Score8 Score9
            print("High Score is:",High)
            print("Low Score is:",Low)
            print("Total Score is:",Total)
            print("Counter is 9")
            exit()      
        Score10=int(input("Enter your 10th score:"))
        if ((Score1 or Score2 or Score3 or Score4 or Score5 or Score6 or Score7 or Score8 or Score9 or Score10)> 300) or ((Score1 or Score2 or Score3 or Score4 or Score5 or Score6 or Score7 or Score8 or Score9 or Score10)< 0):
                print("Please enter a Number from 0 and 300 included!")
                continue

        #list of all the score variables
        ScoreList=[Score1,Score2,Score3,Score4,Score5,Score6,Score7,Score8,Score9,Score10]        
        #total of the scores
        Total=Total (Score1 Score2 Score3 Score4 Score5 Score6 Score7 Score8 Score9 Score10)        
        #highest score
        High = max(ScoreList)
        #lowest score
        Low = min(ScoreList)
        if ((Score1 or Score2 or Score3 or Score4 or Score5 or Score6 or Score7 or Score8 or Score9 or Score10)> 300) or ((Score1 or Score2 or Score3 or Score4 or Score5 or Score6 or Score7 or Score8 or Score9 or Score10)< 0):
                print("Please enter a Number from 0 and 300 included!")
        
    #catches letter in input errors  
    except ValueError:
               
        print('''Sorry, I didn't understand that...Please enter a NUMBER.
Try again!''')
        #if ((Score1 or Score2 or Score3 or Score4 or Score5 or Score6 or Score7 or Score8 or Score9 or Score10)> 300) or ((Score1 or Score2 or Score3 or Score4 or Score5 or Score6 or Score7 or Score8 or Score9 or Score10)< 0):
                #print("Please enter a Number from 0 and 300 included!")
                
        #better try again... Return to the start of the loop
        continue
    
    else:
                          
        #we're ready to exit the loop.
        break

CodePudding user response:

I think you can do something like this:

  • I do not have all the same variables in this you have

      invalid = True
      while invalid:
          number = int(input("> "))
          if number > 300 or number < 0:
              print("that is invalid input")
          else:
              invalid = False
    

CodePudding user response:

The issue arrises from your conditions;

if ((Score1 or Score2)> 300) or ((Score1 or Score2)< 0):

(Score1 or Score2) is like setting a single value, Score1, unless Score1 is None, in which case the value will be Score2.

What I believe you are after is;

if Score1 > 300 or Score2 > 300 or Score1 < 0 or Score2 < 0:

Etc. (Although in order to reach your checks of Score2,3,... you have already checked the previous scores, therefore they do not need to be checked again) The above example can be simplified to;

if 0 > Score2 > 300:
  • Related