0

Hello, I am trying to get my input to only allow integers, and once it gets past 10, it displays error, any assistance would be appreciated.

square_ct = input("Enter an integer from 1-5 the number of squares to draw: ")
triangle_ct = input("Enter an integer from 1-5 the number of triangles to draw: ")

while square_count(input) > 10:
    print ("Error!")
    square_count=input() #the statement reappears

while triangle_count(input) > 10:
    print ("Error!")
    triangle_count=input() #the statement reappears
0

2 Answers 2

3

My preferred technique is to use a while True loop with a break:

while True:
    square_ct = input("Enter an integer from 1-5 the number of squares to draw: ")
    if square_ct <= 10: break
    print "Error"

# use square_ct as normal

Or, on Python 3:

while True:
    square_ct = int(input("Enter an integer from 1-5 the number of squares to draw: "))
    if square_ct <= 10: break
    print("Error")

# use square_ct as normal
Sign up to request clarification or add additional context in comments.

4 Comments

So should I do two separate while true statements for each of the questions? I am getting error TypeError: unorderable types: str() > int()
I suspected as much, but the way he was using input (without converting to int) made it look like Python 2.x.
Am I the only one who thinks it's strange that the text says "enter an integer from 1-5", but the test only involves 10? (Just a detail, of course.)
Yep, I noticed it. Not the weirdest thing I've seen on SO; I'm sure the asker will figure it out.
0

I took the path nneonneo offered and got pretty close to what I wanted. The final result is below.

I thank all of you for your comments. The last time I did anything even slightly like programming was in Fortran on punch cards on an IBM 360.

I apologize for asking such basic questions but really am trying.

The code that works but really doesn't point out exactly which fault happened. I will try to figure out how to convert the string in the input statement to a float and see if there is a remainder (modulo maybe?) so the user gets a better hint what was wrong.

import math
from datetime import datetime
import time

num = 0
start = 0
end = 0

try:
   num = int(input('Enter a positive whole number: '))
   if (num >= 0 and num <= 2147483647):
       start = datetime.now()
       print("The factorial of ", num, " is : ")
       print(math.factorial(int(num)))
       end = datetime.now()
   else:
      print('Number must be between 0 and 2147483647 are allowed.')
      print(f"Time taken in (hh:mm:ss.ms) is {end - start}")
except ValueError:
    print('Text or decimal numbers are not allowed. Please enter a whole number between 0 and 2147483647')

I have a lot to learn because I'm bored...

Norman

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.