Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need some help here
#1
Create a program that gives you a character based on a number of points. The points should be taken from the user. If the number of points is not an integer or is outside the scale, an error message should be printed.

Code:

points = input("Please write your score here: ")
if (100>=points and points>=89):
print ("A")
elif (88>=points and points>=77):
print ("B")
elif (76>=points and points>=65):
print ("C")
elif (64>=points and points>=53):
print ("D")
elif (52>=points and points>=41):
print("E")
elif (40>=points and points>=0):
print("F")
else:
print("please write a score between 0 and 100")

What drives me crazy is how to check if user input is an integer or a float. If it is a float i want an error message to appear in the console, like "Please write an integer". If userinput is an integer it should give the appropriate grade. It should be said that i have only learned the basics in python.

I have tried something like this:
a = ''
while a.isdigit() == False:
a = input('Enter a number: ')
But i have not really learned that yet.
Reply
#2
Please use python tags when posting code. See the BBCode link in my signature (below) for instructions.

I usually check for an integer this way:

while True:
    text = input('Enter a number, please: ')
    if text.strip().isdigit():
        num = int(text)
        break
    print('Integers only, please.')
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Sep-16-2017, 09:26 PM)ichabod801 Wrote: Please use python tags when posting code. See the BBCode link in my signature (below) for instructions.

I usually check for an integer this way:

while True:
    text = input('Enter a number, please: ')
    if text.strip().isdigit():
        num = int(text)
        break
    print('Integers only, please.')

You sir are a genius! Thank you so much!
If I write a negative number it will ask for a number again. So i suppose this method works for positive numbers only. In my case negative numbers are irrelevant anyways.

If i understand the code corrently: The code above will check if text is a digit. If the digit is an integer (because isdigit only accepts integers and not negatives and floats?) it saves it in the varible num. While True means that if everything in the code that follows is infact true, it will break and continue the script.
Reply
#4
To be sure you get it:
  • while True means go forever. A while loop goes until the condition is False. If the condition is a literal True, it goes on forever.
  • text.strip() takes out whitespace that would confuse isdigit. isdigit() makes sure the remaining characters are all digits (0-9).
  • num = int(text) converts text to an integer and stores it in num. We can do this because it's only digits and whitespace. Otherwise we might get an error.
  • break gets out of the loop, because we have what we want.
As you note, it's not perfect, as it misses negative integers. I'm not usually interested in them either. You could solve that with a more complicated expression, or a try/except block.

while True:
    text = input('Enter a number, please: ')
    try:
        num = int(text)
        break
    except ValueError:
        print('Integers only, please.')
The try/except synatx tries the indented block of code. If any errors pop up, it goes to the except and sees if they match. ValueError is what you would get if you tried int('two') or int('2.718').
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020