1

New to programming. Python3. Testing options to check if input is an intiger. Input can be either positive or negative number.

Went for try loop. Problem: if user inputs e.g. a char (letter) two times program goes to next line. I guess I should pack it in another loop or maybe there is a simpler/prettier solution? As input can also be a negative number do not want to use .isdigit()

My code so far:

print("Hi, I will count for you.")

initial_number = input("Please enter the first number ")

try:
    initial_number = int(initial_number)
except ValueError:
    print("Please enter an intiger.")
    initial_number = input("Please enter the first number ")

last_number = input("Please enter the last number ")
try:
    last_number = int(last_number)
except ValueError:
    print("Please enter an intiger. ")
    last_number = input("Please enter the last number")

between = input("Please enter the pseudo-iterator ")
try: 
    between = int(between)
except ValueError:
    print("Please enter an intiger. ")
    between = input("Please enter the pseudo-iterator. ")

for number in range(int(initial_number), int(last_number)+1, int(between)):
    print(number)

edit: Did not realize that loop has not been properly initialized. Thank you for help!

2
  • Which do you want: digits or integers? Commented Jul 23, 2019 at 17:20
  • Intigers. Sorry, English is not my first lg. Thanks for pointing out the difference. Edited the question. Commented Jul 23, 2019 at 17:21

1 Answer 1

1

You haven't written try loops, but doing so looks like it would accomplish what you want. Something like:

while True:
    try:
        initial_number = int(input("Please enter the first number "))
        break
    except ValueError:
        print("Please enter an integer.")
Sign up to request clarification or add additional context in comments.

1 Comment

It does! Thank you for correcting my assumption about having the loop part. :)

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.