Python Forum
[split] Very basic coding issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Very basic coding issue
#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


Messages In This Thread
[split] Very basic coding issue - by aary - Jun-03-2020, 08:47 AM
RE: [split] Very basic coding issue - by Yoriz - Jun-03-2020, 11:17 AM
RE: [split] Very basic coding issue - by buran - Jun-03-2020, 11:24 AM
RE: [split] Very basic coding issue - by buran - Jun-03-2020, 11:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic Coding Question: Exit Program Command? RockBlok 3 624 Nov-19-2023, 06:31 PM
Last Post: deanhystad
  [split] Issue installing selenium Akshat_Vashisht 1 578 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,515 Jul-27-2021, 03:48 PM
Last Post: jefsummers
  Very basic coding issue mstichler 3 2,613 Jun-03-2020, 04:35 AM
Last Post: mstichler
  Basic example Coding gudlur46 2 2,092 Dec-19-2019, 01:55 PM
Last Post: gudlur46
  Python Coding Issue! ankitdixit 3 98,670 Sep-25-2019, 06:31 AM
Last Post: rohanjoshi0894
  Coding issue 1557676 2 33,533 Aug-02-2019, 08:54 PM
Last Post: cvsae
  Basic coding question with Python Than999 3 3,156 Jul-17-2019, 04:36 PM
Last Post: jefsummers
  Basic List Issue rogueakula 1 2,202 May-18-2019, 06:01 PM
Last Post: snippsat
  [split] Is there any issue related to path defined somewhere purnima1 2 2,479 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