0

I am trying to get this input below to work, I want to if the user puts in a number less then 10 to say you need to put a higher number, then get them to input another number. until they pick a number that is accepted.

However, my code does not pick up the result of the user and does pick the correct response or move on when someone say picks 50.

It just loops forever. Is there any way to fix this?

def weapons_(aaa,numguns,numknifes,numbombs,numswords):
    print("Good job!")
    
while True:
    try:
        aaa = input("""Enter you're number accordinly to the list above:""")
        number = int(aaa)
        print("this is a num, thank you")
        while number <=10:
            print ("You need to pick a number between 50 and 200")
            
            
            if number <= 50:
                print(x)
                break
            elif number <= 100:
                print (y )
                break
            elif number<= 150:
                print (m + p)
                break
            elif number <= 200:
                print (z)
                break
    except ValueError:
          print("this is not a number, please try again")
Red
  • 26,798
  • 7
  • 36
  • 58
DojKMGg55
  • 37
  • 3

1 Answers1

0

One thing - please format the code with backticks just before 'def'. The backtick is `, just above escape key on my laptop. Also, its good style to try to only use the three speech marks for multiline comments, you don't need it to designate a string (is what I've been told anyway) :) Finally, most of your number checks in the second code block aren't needed, because no numbers that meet the required conditions will enter the code block - if you want these to be done still then move them away from a conditional that excludes all values of number that can satisfy them (i.e. if you have a number variable with 99, this wont cause y to be printed, because it is above 10, so wont satisfy while number <=10:. This means it wont execute any of the code below while number <=10:).

Anyway, I think the problem is when someone inputs 50 or a good answer, it will print your variable (x if its under 50, y under 100 etc), and then it will ask for user input with """Enter you're number accordinly to the list above:""". Right?

This is happening because your break is only escaping the inner while loop - it will just go back to the outer while loop (i.e. While True) when this happens, and ask everything again. To fix this, try putting:

while True:
    try:
        aaa = input("""Enter you're number accordinly to the list above:""")
        number = int(aaa)
        print("this is a num, thank you")
        
        #####this######
        if number >10:
            #do whatever you want when the user enters a number between 10 and 200.  Break will exit all the while loops
            print ('ok')
            break
        ###############
        
        while number <=10:
            print ("You need to pick a number between 50 and 200")

            #this code will only be entered if number is less than or equal to 10.  So any value of number above 10 wont cause this code to execute.  so all the conditions checking 100-200 are not going to be entered
            if number <= 50:
                print(x)
                break
            elif number <= 100:
                print (y )
                break
            elif number<= 150:
                print (m + p)
                break
            elif number <= 200:
                print (z)
                break
    except ValueError:
          print("this is not a number, please try again")
Tim Kirkwood
  • 598
  • 2
  • 7
  • 18