0

Hi try to do a program that asks the user for a number and only allow an int between 0-100 If not, i want to ask the same question agian. EDIT I try to do a program that asks a user for a number then the user have to type a nuber between 0 - 100 if not question i asked agin. If the user type a sting it will generates a other message and then ask the user to enter a number between 0 -100. if the nuber is correct i want to break out of the loop. EDIT2 Problem solved! Thanx for quick help from every one. I am a noob on this...

print('Whats your name?')
name = input()
print('Hello ' + name + ' enter a number between 0 - 100')

number = input()
while True:
    try:
        if  0 <= int(number) <= 100:
            print('Good')
            break
        else:
            print('You must enter a nuber between 0 - 100')
            continue
    except ValueError:
            print('You must enter a nuber between 0 - 100')
Maak
  • 3
  • 3

4 Answers4

3

You need to use while num < 0 or num > 100

print("What's your name?")
name = input()

print('Hello ' + name + ' enter a number between 0 - 100')

try:
    num = int(input())
except ValueError:
    print('You must enter a number between 0 - 100')

while num < 0 or num > 100:
    try:
        num = int(input())
    except ValueError:
        print('You must enter a number between 0 - 100')

print('Good')

Example of output:

What's your name?
'John Doe'
Hello John Doe enter a number between 0 - 100
200
120
300
50
Good
ettanany
  • 19,038
  • 9
  • 47
  • 63
  • I understand this program. But i also want to tell the user that they type a number out of range and also handle if they writ a string. Thanx for your support! – Maak Dec 16 '16 at 11:07
  • Take a look at the edit, I think that's what you want. – ettanany Dec 16 '16 at 11:10
2

You need to take the input() inside the while loop. Here is a verbose code:

def take_int_as_input(default=-1):
    try:  
        return int(input())
    except:
        return default

print('enter a number between 0 - 100')
num = take_int_as_input()
while 0 > num or num > 100:
    print('You must enter a nuber between 0 - 100')
    num = take_int_as_input()
else:
    print "Good"

Paste the above code in a file with a name code.py. The following is the output:

$ python code.py 
enter a number between 0 - 100
d
You must enter a nuber between 0 - 100
gf
You must enter a nuber between 0 - 100
rfe
You must enter a nuber between 0 - 100
876543
You must enter a nuber between 0 - 100
22
Good

A concise and less verbose version:

print('enter a number between 0 - 100')
while not (0 <= take_int_as_input() <= 100):
    print('You must enter a nuber between 0 - 100')
else:
    print "Good"
Pankaj Singhal
  • 15,283
  • 9
  • 47
  • 86
1

try this code

print('Whats your name?')
name = input()

while True:
    number = input()
    try:
        if 0 <= int(number) <= 100:
            print('Good')
            break
    except ValueError:
        print('You must enter a number between 0 - 100')

input() should be inside while loop

minji
  • 512
  • 4
  • 16
0

You can use a function defined by a recurrence :

def ask():
    num = int(input("Enter your number between 0 and 100"))
    if num < 0 or num > 100:
        ask()
    else:
        print('Good')
        pass 
MMF
  • 5,750
  • 3
  • 16
  • 20
  • 1
    It's generally a bad idea to use recursion when a simple loop will do, because Python doesn't eliminate [tail calls](https://en.wikipedia.org/wiki/Tail_call), and there's a recursion depth limit. – PM 2Ring Dec 16 '16 at 11:04
  • @PM2Ring Thanks a lot for your feedback ! – MMF Dec 16 '16 at 11:10