'How to make my program start at 1 and double that amount and also print the total
My problem is that my code starts 1 day at 2 pennies, it should start at 1. I also would like to know how to print the total amount of pennies. Here is the code I have so far:
daysworked = int(input("Enter the amount of days you worked: "))
pay = 1
print("Day\tPay")
print("-----------------")
for daysworked in range (1, daysworked+1):
endpay = pay * 2
print(daysworked, "\t", endpay)
pay = endpay
Solution 1:[1]
If you want to print out the total amount of money the user has earned over the course of the days they worked, you can create a variable: total, which is the sum of all the payments earned per day.
Also, since on the first day, you don't want to double your value, we can simply print out the first value before the loop (skipping an iteration of your loop, hence the -1), or start our variable pay at 0.5.
To integrate this into your code, we can utilize the for loop you implemented and simply add up all the endpay values.
Other minor changes you could make: As @S3DEV mentioned, it is unnecessary to create a secondary variable to store updated payment, you can simply update the variable pay! We could also edit the for loop to just take in one parameter, the daysworked, instead of a starting and ending value. In Python, it is also recommended that if your variable names are multiple words long, separate them with an underscore.
With these changes included, your code could look something like this:
days_worked = int(input("Enter the amount of days you worked: "))
pay = 1
total = 1
print("Day\tPay")
print("-----------------")
print(1,pay)
for days_worked in range (days_worked - 1):
pay *= 2
total += pay
print(days_worked + 1, "\t", pay)
print("Total Payment:" , total)
Different Solution: (This solution will result in float answers since pay starts at .5, so you can cast to an int):
days_worked = int(input("Enter the amount of days you worked: "))
pay = .5
total = 0
print("Day\tPay")
print("-----------------")
for days_worked in range (days_worked):
pay *= 2
total += pay
print(days_worked, "\t", int(pay))
print("Total Payment:" , int(total))
I hope this helped with your problem! Let me know if you need any further clarification or details :)
Solution 2:[2]
I'm guessing you don't want to print in every iteration, and that's what you are asking. Just unindent that print line so it executes at the end of the loop. Also I removed superfluous variable.
daysworked = int(input("Enter the amount of days you worked: "))
pay = 1
print("Day\tPay")
print("-----------------")
for daysworked in range (1, daysworked+1):
pay = pay * 2
print(daysworked, "\t", pay)
Solution 3:[3]
daysworked = int(input("Enter the amount of days you worked: "))
pay = 1
endpay = 0
print("Day\tPay")
print("-----------------")
for dayworked in range (1, daysworked+1):
pay *= 2
print(dayworked, "\t", endpay)
endpay += pay
print(endpay)
Have a look at two things here.
First, you need to add one more variable to keep track of the sum, endpay in this case, which you have to print at the end. Although, in this case, endpay will always be the double of the last value of pay less two. ;) The last line could as well have been written print(2 * pay - 2) and get rid of the lines using 'endpay'.
Second the difference between daysworked and dayworked (you could have used i or any other variable name here as well).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | JNevill |
| Solution 3 |
