-3

I'm trying to make a program in python so that when I input a number from 1 to 10, a specific set of program goes on and asks for another number from 1 to 10 and runs another program, until I enter 0(zero) and the program stops.

So my guess was using a while loop but it didn't quite work out.

user_input = input()
user_input = int(user_input)
while user_input != 0:
    (program)
else:
    quit()
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Sun
  • 13
  • 3
  • Do you mean another python file by saying "another program"? Or it can be a function? – GooDeeJAY Mar 30 '21 at 13:36
  • Nope. In just one python file. That program I talked about is actually a really simple program that shows a simple multiplication table for the number I input. – Sun Mar 30 '21 at 13:38
  • Related: [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), [Execute a function to return a value on a loop until that function returns False - Python](https://stackoverflow.com/questions/37924937/execute-a-function-to-return-a-value-on-a-loop-until-that-function-returns-false), [I need to call a function until it returns 0 in python](https://stackoverflow.com/questions/5574661/i-need-to-call-a-function-until-it-returns-0-in-python) – wwii Mar 30 '21 at 13:41
  • Thanks a lot guys! I got it solved! – Sun Mar 30 '21 at 13:58

5 Answers5

1

Try this:

 user_input = int(input())
 while user_input != 0:
     (program)
     user_input = int(input())
 quit()

With your current code you only ask for input once so the loop won't end. This way you can input a new number after every iteration.

Seppeke
  • 143
  • 7
1

Your current program only asks once and then the loop keeps repeating. You need to keep asking for input inside the loop.

def program():
  print("Executing Task....")

user_input = int(input())

while user_input != 0:
    program()
    user_input = int(input())

printf("Program Terminated")
Iftieaq
  • 1,904
  • 2
  • 15
  • 26
0

Here it is:

def program():
    pass


user_input = int(input())

while user_input:
    program()
    user_input = int(input())

quit(0)
GooDeeJAY
  • 1,681
  • 2
  • 20
  • 27
0

A different way using iter with a sentinel:

def program(number):
    if number < 0 or number > 10:
        print('Invalid number:', number)
    else:
        print('Valid number:', number)

def quit():
    print('quitting')

def get_number():
    return int(input('Enter a number from 1 to 10: '))

for number in iter(get_number, 0):
    program(number)
else:
    quit()
Booboo
  • 38,656
  • 3
  • 37
  • 60
0
not_zero = True

while not_zero:
    num = int(input("Enter a number: "))
    if num == 0:
        not_zero = False

you can stop your loop using a boolean value.

MZ Mahid
  • 15
  • 4