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
#1
1. How to define range 1 to infinity in function range()?
2. Am I using for-else loop correctly?
3. Also even if I enter everything correctly following is the result:

Your age in 1 will be: 1

Thanks

print('\nThis is a program to detemine your age in any year.\n')
currentAge = currentYear = futureAge = futureYear = 0

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

for currentAge in range(1,95):
    break

else:
    currentAge = int(input('\nEnter your real age (1 - 95): '))


currentYear = int(input('\nEnter current year: '))

for currentYear in range(1, 9999999):
    break

else:
    currentYear = int(input('Please enter valid year: '))


futureYear = int(input('\nEnter the year to determine your age in that year: '))

for futureYear in range(1, 9999999):
    break

else:
    futureYear = int(input('Please enter valid year: '))

futureAge = currentAge + (futureYear - currentYear)

if futureAge > 0:

    if futureAge > 95 and futureYear > currentYear:
        print('\nYour age in', futureYear, 'will be:', futureAge)
        print('\nAre you sure you are going to be alive by then!')

    elif futureYear < currentYear:
        print('\nYour age in', futureYear, 'was:', futureAge)

    else:
        print('\nYour age in', futureYear, 'will be:', futureAge)

else:
    print('\nYou weren\'t alive in', futureYear, ".")
Reply
#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
#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


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