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
#2
To implement an infinite loop, you'll need a while loop instead of a for loop. One of the purposes of a for loop is to preclude infinite looping.

Now, there are other issues with your code. Lines 4 through 6:

currentAge = int(input('\nEnter your current age in years: '))

for currentAge in range(1,95):
currentAge is instantiated on line 4 and then reset on line 6. In a for loop, the variable following "for" is set to the current value picked out of range() (or other iterable). So, once line 6 runs, currentAge becomes 1, then 2, etc. as the loop executes. This pattern repeats in your code and is likely contributing to the problem.

Now, I'm guessing you intended to check if those values are within the range(). If that's the case, you should do this:

currentAge = int(input('\nEnter your current age in years: '))

if currentAge in range(1,95):
Reply


Messages In This Thread
RE: [Beginner] Code is not producing desired result - by stullis - Mar-20-2020, 03:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner: Code not work when longer list raiviscoding 2 951 May-19-2023, 11:19 AM
Last Post: deanhystad
  Json filter is not capturing desired key/element mrapple2020 1 1,238 Nov-24-2022, 09:22 AM
Last Post: ibreeden
  help me simple code result min and max number abrahimusmaximus 2 1,006 Nov-12-2022, 07:52 AM
Last Post: buran
  Filter and str.isdigit producing an error tester_V 5 2,169 Aug-12-2022, 07:50 AM
Last Post: Gribouillis
  Can a program execute code in iPython shell and get result? deanhystad 3 1,858 Jun-17-2022, 03:45 AM
Last Post: Larz60+
  pyautogui.locateOnScreen producing error dude8074 6 4,149 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,633 Apr-06-2022, 03:41 PM
Last Post: Gribouillis
  how can I display only desired items? 3lnyn0 5 2,210 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,691 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  producing numbers out of a list bouraque7878 10 3,947 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