Python Forum
[split] Very basic coding issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Very basic coding issue
#1
age = int(input('please enter your age: '))
try:
    if age > 0:
        print("You are", age, "years old!")
    elif age > 90:
        print('you are too old!')
except:
    print('that can\'t be your age')
Reply
#2
Did you have a question?
Reply
#3
Maybe you want to swap the if and elif - first check if age > 90 and then elif age > 0
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
age = input('please enter your age: ')
while True : 
    try:
        age = int(age)
        if age > 0 and age < 90:
            print("You are", age, "years old!")
            break
        elif age > 90:
            print('you are too old!')
            break
    except ValueError:
        print('that can\'t be your age')
        break
3 different outputs :
Output:
please enter your age: g that can't be your age
Output:
please enter your age: 99 you are too old!
Output:
please enter your age: 10 You are 10 years old!
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#5
actually it's better to have minimal number of lines in the try block, i.e. where you expect the error. Also, the input must be inside the loop (otherwise, why would you have a loop at all, if you break out in any case):
while True :
    age = input('please enter your age: ')
    try:
        age = int(age)
    except ValueError:
        print('that can\'t be your age')
        continue
    if age > 0 and age < 90:
            print("You are", age, "years old!")
    elif age > 90:
        print('you are too old!')
    break
or using else

while True :
    age = input('please enter your age: ')
    try:
        age = int(age)
    except ValueError:
        print('that can\'t be your age')
    else:
        if age > 0 and age < 90:
                print("You are", age, "years old!")
        elif age > 90:
            print('you are too old!')
        break
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic Coding Question: Exit Program Command? RockBlok 3 584 Nov-19-2023, 06:31 PM
Last Post: deanhystad
  [split] Issue installing selenium Akshat_Vashisht 1 551 Oct-18-2023, 02:08 PM
Last Post: Larz60+
  [split] Is there some coding that will do the following in 3 separate events? bensan 23 6,387 Jul-27-2021, 03:48 PM
Last Post: jefsummers
  Very basic coding issue mstichler 3 2,604 Jun-03-2020, 04:35 AM
Last Post: mstichler
  Basic example Coding gudlur46 2 2,066 Dec-19-2019, 01:55 PM
Last Post: gudlur46
  Python Coding Issue! ankitdixit 3 92,592 Sep-25-2019, 06:31 AM
Last Post: rohanjoshi0894
  Coding issue 1557676 2 31,391 Aug-02-2019, 08:54 PM
Last Post: cvsae
  Basic coding question with Python Than999 3 3,126 Jul-17-2019, 04:36 PM
Last Post: jefsummers
  Basic List Issue rogueakula 1 2,195 May-18-2019, 06:01 PM
Last Post: snippsat
  [split] Is there any issue related to path defined somewhere purnima1 2 2,449 Sep-05-2018, 06:28 AM
Last Post: purnima1

Forum Jump:

User Panel Messages

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