Home > Blockchain >  I can't figure out this input
I can't figure out this input

Time:12-01

num = 0

def calculate1(player1, num):
    if player1 == 1:
        num = num   player1
        print(f"The number is {num}")
        return (num)
    elif player1 == 2:
        num = num   player1
        print(f"The number is {num}")
        return (num)
    elif player1 == 3:
        num = num   player1
        print(f"The number is {num}")
        return (num)
    else:



#yrn = yes or no
yrn = input("Are you going to play game? (Y/N) : ").upper()
if yrn == "Y":
    player1 = int(input("How many numbers are you going to add? : "))
    num = calculate1(player1, num)

I want to make that if I type more than 3, the programme ask one more time to reenter the number. Please help meeeee

CodePudding user response:

You can use while & check condition inside the while whenever user inputs.

num = 0

def calculate1(player1, num):
    if player1 == 1:
        num = num   player1
        print(f"The number is {num}")
        return (num)
    elif player1 == 2:
        num = num   player1
        print(f"The number is {num}")
        return (num)
    elif player1 == 3:
        num = num   player1
        print(f"The number is {num}")
        return (num)
    



#yrn = yes or no
yrn = input("Are you going to play game? (Y/N) : ").upper()
if yrn == "Y":
    
    while 1:
        player1 = int(input("How many numbers are you going to add? : "))
        
        if player1 >3:
            print("Should be less than 3 enter again")
            pass
        else:
            
            num = calculate1(player1, num)
            break

Sample outputs #

Are you going to play game? (Y/N) : y
How many numbers are you going to add? : 4
Should be less than 3 enter again

How many numbers are you going to add? : 3
The number is 3

CodePudding user response:

If you only want to ask once:

#yrn = yes or no
yrn = input("Are you going to play game? (Y/N) : ").upper()
if yrn == "Y":
    player1 = int(input("How many numbers are you going to add? : "))
    if player1 > 3:
        player1 = int(input("How many numbers are you going to add? : "))
    num = calculate1(player1, num)

But you if you want to keep asking:

#yrn = yes or no
yrn = input("Are you going to play game? (Y/N) : ").upper()
player1 = 10  # just larger than 3
if yrn == "Y":
    while player1 > 3:
        player1 = int(input("How many numbers are you going to add? : "))
    num = calculate1(player1, num)

If you want to make it more fool proof (i.e. your code should never break) you can also update the function:

def calculate1(player1, num):
    if player1 == 1:
        num = num   player1
        print(f"The number is {num}")
        return (num)
    elif player1 == 2:
        num = num   player1
        print(f"The number is {num}")
        return (num)
    elif player1 == 3:
        num = num   player1
        print(f"The number is {num}")
        return (num)
    else:
        return None



#yrn = yes or no
yrn = input("Are you going to play game? (Y/N) : ").upper()
if yrn == "Y":
    num = 0
    while True:
        player1 = input("How many numbers are you going to add? : ")
        try:
            player1 = int(player1)
            num = calculate1(player1, num)
        except:
            if player1.lower() == 'quit' or player1.lower() == 'q':
                print('Bye')
                break
            num = None
        if num is not None:
            break  # break while loop
  • Related