Python Forum

Full Version: [Beginner] Code is not producing desired result
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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, ".")
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):
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.