yandex
loader

please wait

  • Lucky Negi Mar-15-2019 06:39:31 AM ( 1 week ago )

     

    I am making a text based adventure game in python 3 and I was wondering what the simplest loop is. Using the code I have, it continues to print "whats the number" even when you put the correct number, also giving 9 as input doesnt work. It also doesn't work when I give ("8","9"). Here is my code :

    print("whats the number?")
    required_number = ("8" or "9")
    
    while True:
        number = input()
        if number == required_number:
            print ("GOT IT")
        else: print ("Wrong number try again")
  • Jasmin Bhasin Mar-15-2019 06:41:00 AM ( 1 week ago )

    Try this :

    print("whats the number?")
    required_number = [8,9]
    while True:
        number = int(input())
        if number in required_number :
            print('GOT IT')
            break
        else:
            print('Wrong number try again')

    Sample output in shell :

    whats the number?
    5
    Wrong number try again
    2
    Wrong number try again
    4
    Wrong number try again
    8
    GOT IT
  • Nageshwer Reddy Mar-15-2019 06:52:42 AM ( 1 week ago )

    If your required_number or the input will accommodate a string, then you can use this:

    required_number = [8,9]
    required_number = str(required_number)
    number = None
    
    while True:
        number = input("Write a number: ")
        if number in required_number:
            print ("GOT IT")
        else: 
            print ("Wrong number try again")

    Output:

    Write a number: 3
    Wrong number try again
    Write a number: 8
    GOT IT
    Write a number: Hi
    Wrong number try again

Please login

Similar Discussion

Recommended For You