Python Forum
[Beginner] Code is not producing desired result
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Beginner] Code is not producing desired result
#3
It looks you are confusing loops and if conditions.
Loops are used to repeat execution of a code block.
If condition is used to branch code execution (i.e. execute one block or another depending on one or more conditions).

At the moment each of the loops on lines 6-28 would try to iterate over certain range, but will break out of the loop immediately (in the first iteration). If you want to validate user input, you should use if block, not for loop. And you may wrap that if block in a while loop, e.g.:
while True:
    age = int(input('Enter your age (1-95):'))
    if 1 <= age <= 95: # it is arguable why you use these boundaries, not just if age >= 0:
        break
    else:
        print('Not valid age')
print(f'your age is {age}')
Output:
Enter your age (1-95):100 Not valid age Enter your age (1-95):25 your age is 25
Please, note that this is just an example implementation, it could be done in different ways, have more validations/error handling, etc.
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
RE: [Beginner] Code is not producing desired result - by buran - Mar-21-2020, 10:26 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner: Code not work when longer list raiviscoding 2 848 May-19-2023, 11:19 AM
Last Post: deanhystad
  Json filter is not capturing desired key/element mrapple2020 1 1,164 Nov-24-2022, 09:22 AM
Last Post: ibreeden
  help me simple code result min and max number abrahimusmaximus 2 946 Nov-12-2022, 07:52 AM
Last Post: buran
  Filter and str.isdigit producing an error tester_V 5 2,002 Aug-12-2022, 07:50 AM
Last Post: Gribouillis
  Can a program execute code in iPython shell and get result? deanhystad 3 1,775 Jun-17-2022, 03:45 AM
Last Post: Larz60+
  pyautogui.locateOnScreen producing error dude8074 6 3,901 Apr-17-2022, 05:05 PM
Last Post: bowlofred
  code running for more than an hour now, yet didn't get any result, what should I do? aiden 2 1,539 Apr-06-2022, 03:41 PM
Last Post: Gribouillis
  how can I display only desired items? 3lnyn0 5 2,100 Dec-25-2021, 06:49 PM
Last Post: menator01
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,645 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  producing numbers out of a list bouraque7878 10 3,816 Nov-12-2021, 09:13 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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