Home > Mobile >  How to code a data validation loop that consist of two numbers?
How to code a data validation loop that consist of two numbers?

Time:07-08

Here is my code here I've already coded the validation loop but I'm supposed to code a data validation loop to ensure that the number of days must be between 2 and 40 (Both 2 and 40 are valid) It works so that any number less than two is invalid and prints the message but numbers above 40 it does not print the error message.

dayPennies = 1

num_days = 0

total = 0.0


num_days = int(input('Enter the number of days: '))

while num_days < 2 and num_days > 40: 

num_days = int(input("Your entered number of days is invalid"))  

print ('Day  \t Pennies Earned for day')
print ('_______________________________')

for day in range (1, num_days   1):
  print(day, '\t$', float(dayPennies / 100))
  total  = dayPennies
  dayPennies *=2


print('The total salary for', num_days, \
      'days is: $', float(total/100))

CodePudding user response:

you've entered the wrong condition in your while loop, it should be

while num_days < 2 and num_days > 40:
#rest of your code

EDIT I know you said it worked but this is just a word of advice. When doing a project like this, it's best to put your calculations in a function and call your input outside of that function until you get the correct answer... then from there you put the correct answer as a parameter in that function. Here's what I mean.

from tkinter.tix import Tree

num_days = 0

def calculatePennies(num_days):
    dayPennies = 1
    total = 0.0

    print ('Day  \t Pennies Earned for day')
    print ('_______________________________')

    for day in range (1, num_days   1):
        print(day, '\t$', float(dayPennies / 100))
        total  = dayPennies
        dayPennies *=2


    print('The total salary for', num_days, \
        'days is: $', float(total/100))


num_days = int(input('Enter the number of days: '))

while num_days < 2 or num_days > 40:
    num_days = int(input("Your entered number of days is invalid"))

calculatePennies(num_days)

As you can see I put calculatePennies as a function, it's cleaner and more readable while being easier to go back and change. Hope this helps

CodePudding user response:

Here is a fixed and more Pythonic way to write your code:

ps = 1
total = 0
valid_days_range = range(2, 41)

days = int(input('Enter the number of days: '))

while days not in valid_days_range:
    print('Your entered number of days is invalid!')
    days = int(input('Enter the number of days: '))

print('Day\t$ Pennies Earned for day')
print('---\t------------------------')

for day in range(days):
    print(f'{day   1}\t$ {ps / 100:0,.2f}')
    total  = ps
    ps *= 2

print(f'The total salary for {days} days is: $ {total / 100:0,.2f}')

For days = 19, it will yield the following output:

Enter the number of days: 19
Day $ Pennies Earned for day
--- ------------------------
1   $ 0.01
2   $ 0.02
3   $ 0.04
4   $ 0.08
5   $ 0.16
6   $ 0.32
7   $ 0.64
8   $ 1.28
9   $ 2.56
10  $ 5.12
11  $ 10.24
12  $ 20.48
13  $ 40.96
14  $ 81.92
15  $ 163.84
16  $ 327.68
17  $ 655.36
18  $ 1,310.72
19  $ 2,621.44
The total salary for 19 days is: $ 5,242.87

Another option is to use an inequality instead:

ps = 1
total = 0

days = int(input('Enter the number of days: '))

while not (2 <= days <= 40):
    print('Your entered number of days is invalid!')
    days = int(input('Enter the number of days: '))

print('Day\t$ Pennies Earned for day')
print('---\t------------------------')

for day in range(days):
    print(f'{day   1}\t$ {ps / 100:0,.2f}')
    total  = ps
    ps *= 2

print(f'The total salary for {days} days is: $ {total / 100:0,.2f}')
  • Related