#๐ Need help on my code
150 messages ยท Page 1 of 1 (latest)
@glacial marsh
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
cont_value = float(input('Enter your FSA contribution for the year as a whole number (max allowed is 3200): '))
exp_value = float(input('Enter your FSA expenditure for the year as a whole number (max allowed is 3200): '))
difference = ''
while cont_value != 0 and exp_value != 0:
if cont_value - exp_value == 0:
print(f'difference is same')
elif cont_value - exp_value < 0:
difference = (cont_value - exp_value) * -1
elif cont_value - exp_value > 0:
difference = (cont_value - exp_value)
print(f'the difference is ${difference:.2f}')
cont_value = float(input('Enter your FSA contribution for the year as a whole number (max allowed is 3200): '))
exp_value = float(input('Enter your FSA expenditure for the year as a whole number (max allowed is 3200): '))
Here is my code
the issue is after entering in the cont_value and exp_value it calculates the difference Immediately
The code is supposed to continue to ask the cont_value until the user inputs 0
after the user inputs 0 it should add all the previous cont_values together
It should do the same for exp_value
Here is an example of what it should look like:
Enter your FSA contribution for the year as a whole number (max allowed is 3200): 1000
Enter the FSA expenditure as a whole number (enter 0 to finish): 100
Enter the FSA expenditure as a whole number (enter 0 to finish): 300
Enter the FSA expenditure as a whole number (enter 0 to finish): 400
Enter the FSA expenditure as a whole number (enter 0 to finish): 200
Enter the FSA expenditure as a whole number (enter 0 to finish): 0
You have a count of 4 FSA expenditures for the year.
You have accumulated a total of $1000 for the year.
Your expenditure total is the same as your FSA contribution.
I also need help on counting how many inputs the user inputs for expenditures to display at the end
Here is another case:
Enter your FSA contribution for the year as a whole number (max allowed is 3200): 1000
Enter the FSA expenditure as a whole number (enter 0 to finish): 1050
Enter the FSA expenditure as a whole number (enter 0 to finish): 50
Enter the FSA expenditure as a whole number (enter 0 to finish): 0
You have a count of 2 FSA expenditures for the year.
You have accumulated a total of $1100 for the year.
Your expenditure total is $100 over your FSA contribution for the year.
Sorry if its a lot reading I didnt know how to shorten it
cont_value = 1
exp_value = 1
difference = ''
while cont_value != 0 and exp_value != 0:
cont_value = float(input('Enter your FSA contribution for the year as a whole number (max allowed is 3200): '))
exp_value = float(input('Enter your FSA expenditure for the year as a whole number (max allowed is 3200): '))
if cont_value - exp_value == 0:
print(f'difference is same')
elif cont_value - exp_value < 0:
difference = (cont_value - exp_value) * -1
print(f'You are {difference:.2f} above your cont')
elif cont_value - exp_value > 0:
difference = (cont_value - exp_value)
print(f'the difference is below ${difference:.2f}')
cont_value = float(input('Enter your FSA contribution for the year as a whole number (max allowed is 3200): '))
exp_value = float(input('Enter your FSA expenditure for the year as a whole number (max allowed is 3200): '))
There is also another version that I tried but there are still errors occuring
To accomplish the task you can either update the final value as soon as you get a new input, or you can save all of the inputs in a data structure such as a list and then you can just sum them up at the end (this would allow you to show the user all of their inputs if you wanted to add in that functionality). Alternatively, you need some additional variable to keep hold of the total expenditures so you can print them to the user at the end, finding just the difference is not enough here. I'm going to try and run the code now
which code is the latest version you're working on?
This is the latest version
first thing I've noticed is that you're asking for contributions in the while loop, but in the examples they are only asking for contributions once at the beginning, right?
Yess
So I should remove that from the while loop as it is just 1 question
and get the value before the while loop?
and you're asking the same question twice in that loop, I assume you were dealing with the issue that cont_value and exp_value are not defined before the loop, so it doesn't let you check if they are zero, right?
right, with a while loop you have a few options, you can set an initial value which then allows the while loop to begin, or you can do something like
while True: #repeats infinitely
... #your code
if exp_value == 0:
break #stop the loop
honestly, it's up to you which approach you want to take
cont_value = float(input('Enter your FSA contribution for the year as a whole number (max allowed is 3200): '))
exp_value = 1
difference = ''
while exp_value != 0:
exp_value = float(input('Enter your FSA expenditure for the year as a whole number (max allowed is 3200): '))
if cont_value - exp_value == 0:
print(f'difference is same')
elif cont_value - exp_value < 0:
difference = (cont_value - exp_value) * -1
print(f'You are {difference:.2f} above your cont')
elif cont_value - exp_value > 0:
difference = (cont_value - exp_value)
print(f'the difference is below ${difference:.2f}')
exp_value = float(input('Enter your FSA expenditure for the year as a whole number (max allowed is 3200): '))
So I just updated it to this
But the question for exp_value does not repeat and it still automatically calculates the difference
cool, now tell me, how many times do these lines repeat in the example above
You have a count of 2 FSA expenditures for the year.
You have accumulated a total of $1100 for the year.
Your expenditure total is $100 over your FSA contribution for the year.
0 times as i do not have a calculation for total and the count of expenditures inputted from the user
But 1 time for the last statement
I'm talking about the example from your teacher
right, so that part will be outside of the while loop
so would I move my if elif elif statement outside the while loop?
and just keep in exp_value
you don't need to do any of those prints
print(f'difference is same')
print(f'You are {difference:.2f} above your cont')
print(f'the difference is below ${difference:.2f}')
``` in the while loop
the while loop just asks for expenditures and either saves those expenditures in a list, or calculates the difference
cont_value = float(input('Enter your FSA contribution for the year as a whole number (max allowed is 3200): '))
exp_value = 1
difference = ''
while exp_value != 0:
exp_value = float(input('Enter your FSA expenditure for the year as a whole number (max allowed is 3200): '))
if cont_value - exp_value == 0:
print(f'difference is same')
elif cont_value - exp_value < 0:
difference = (cont_value - exp_value) * -1
print(f'You are {difference:.2f} above your cont')
elif cont_value - exp_value > 0:
difference = (cont_value - exp_value)
print(f'the difference is below ${difference:.2f}')
I would suggest saving them in a list because you need to print how many expenditures the user had in the end, which would be the same as the length of that list
So i need to save them in a list in the while loop?
go for it
Could you send me an example of how to store the user inputs in a listr
have you not been taught lists yet?
nope
would you prefer trying a solution without them or do you want me to teach you?
If you could teach me that would be helpful
ok, for the purpose of this task, there are 5 useful things you can do
1.
my_list = []
my_list = list() #both of these create an empty list
2.
print(my_list) #prints the list to your console
3.
my_list.append(<something>) #adds <something> at the end of a list
#notice I did NOT do my_list = my_list.append(<something>)
4.
how_many_elements = len(my_list) #returns an integer telling you how many things are in your list
5.
result = sum(my_list) #sums up all numbers in your list and returns the answer
with these you should be able to complete the assignment
So creating the list how would I create it to sum upp all the inputs
I see
result = sum(my_list)
Which would sum it but how do I set the list to the user inputs
get the number from the user just like you've been doing, then append those numbers to the list one by one in the loop
I think Im getting really close
cont_value = float(input('Enter your FSA contribution for the year as a whole number (max allowed is 3200): '))
exp_value = 1
difference = ''
while exp_value != 0:
exp_value = float(input('Enter your FSA expenditure for the year as a whole number (max allowed is 3200): '))
my_list = list({exp_value})
how_many_elements = len(my_list)
print(f'you inputted {how_many_elements} times')
if cont_value - exp_value == 0:
print(f'difference is same')
elif cont_value - exp_value < 0:
difference = (cont_value - exp_value) * -1
print(f'You are {difference:.2f} above your cont')
elif cont_value - exp_value > 0:
difference = (cont_value - exp_value)
print(f'the difference is below ${difference:.2f}')
For some reason the length of my list is only 1
and does not change based on user inputs
I moved the my_list and how_many_elements into the while loop and it still displays 1
you're doing a bit backwards, try creating an empty list first, then add stuff to it in the while loop, instead of creating your list after the loop
How do I add stuff to it in the while loop
.append
think of .append like + in 1 + 2
except in lists it just appends the number at the end, it doesn't do any calculation
my_list = list()
cont_value = float(input('Enter your FSA contribution for the year as a whole number (max allowed is 3200): '))
exp_value = 1
difference = ''
my_list = list({exp_value})
while exp_value != 0:
exp_value = float(input('Enter your FSA expenditure for the year as a whole number (max allowed is 3200): '))
my_list.append(exp_value)
how_many_elements = len(my_list)
print(f'you inputted {how_many_elements} times')
if cont_value - exp_value == 0:
print(f'difference is same')
elif cont_value - exp_value < 0:
difference = (cont_value - exp_value) * -1
print(f'You are {difference:.2f} above your cont')
elif cont_value - exp_value > 0:
difference = (cont_value - exp_value)
print(f'the difference is below ${difference:.2f}')
I feel like I am super close
The number of inputs now changes but it is wrong
let's take a break and think about a few things in the code
my_list = list({exp_value}) do you need this?
difference = '' do you need this?
how_many_elements = len(my_list) do you need to find out the length in the loop or after the loop is done?
after the loop is done
cool, make those changes
Ok after making the changes it is still off
It counts the last input of 0 as an input Im assuming
my_list = list()
cont_value = float(input('Enter your FSA contribution for the year as a whole number (max allowed is 3200): '))
exp_value = 1
while exp_value != 0:
exp_value = float(input('Enter your FSA expenditure for the year as a whole number (max allowed is 3200): '))
my_list.append(exp_value)
how_many_elements = len(my_list)
total = sum(my_list)
print(f'you inputted {how_many_elements} times')
print(f'You have accumulated a total of ${total} for the year.')
if cont_value - exp_value == 0:
print(f'difference is same')
elif cont_value - exp_value < 0:
difference = (cont_value - exp_value) * -1
print(f'You are {difference:.2f} above your cont')
elif cont_value - exp_value > 0:
difference = (cont_value - exp_value)
print(f'the difference is below ${difference:.2f}')
cool, so when you do 0 as an input, what happens in the code?
it ends the while loop
anything else?
it also sets one of the exp_values to zero
as it is the number based off user input
what happens first:
exp_valuegets added tomy_listexp_valuevalue is checked andwhileloop is stopped
right, if exp_value = 0, is that what we want?
No
what can we do to prevent it?
yep
you can remove the last item in a list using .pop()
!e
my_list = [1,2,3]
my_list.pop()
print(my_list)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
[1, 2]
so
I think I got it
my_list = list()
cont_value = float(input('Enter your FSA contribution for the year as a whole number (max allowed is 3200): '))
exp_value = 1
while exp_value != 0:
exp_value = float(input('Enter your FSA expenditure for the year as a whole number (max allowed is 3200): '))
my_list.append(exp_value)
my_list.pop()
number_exp = len(my_list)
total = sum(my_list)
print(f'You have a count of {number_exp} FSA expenditures for the year.')
print(f'You have accumulated a total of ${total} for the year.')
if cont_value - exp_value == 0:
print(f'Your expenditure total is the same as your FSA contribution.')
elif cont_value - exp_value < 0:
difference = (cont_value - exp_value) * -1
print(f'Your expenditure total is ${difference} over your FSA contribution for the year.')
elif cont_value - exp_value > 0:
difference = (cont_value - exp_value)
print(f'Your expenditure total is ${difference} under your FSA contribution for the yea')
So I am still having issues
but it is the calculation
The list stuff works
The pop does work and succesfully counts the number of expenditures
nice! ๐
How would I be able to fix the calculations
I reread my code and it seems like it should work
what is not working as you would expect
Enter your FSA contribution for the year as a whole number (max allowed is 3200): 1000
Enter your FSA expenditure for the year as a whole number (max allowed is 3200): 100
Enter your FSA expenditure for the year as a whole number (max allowed is 3200): 900
Enter your FSA expenditure for the year as a whole number (max allowed is 3200): 0
You have a count of 2 FSA expenditures for the year.
You have accumulated a total of $1000.0 for the year.
Your expenditure total is $1000.0 under your FSA contribution for the yea
I dont know what it is doing but its not calculating the right difference
my_list = list()
cont_value = float(input('Enter your FSA contribution for the year as a whole number (max allowed is 3200): '))
exp_value = 1
while exp_value != 0:
exp_value = float(input('Enter your FSA expenditure for the year as a whole number (max allowed is 3200): '))
my_list.append(exp_value)
my_list.pop()
number_exp = len(my_list)
total = sum(my_list)
print(f'You have a count of {number_exp} FSA expenditures for the year.')
print(f'You have accumulated a total of ${total} for the year.')
difference = (cont_value - exp_value)
if difference == 0:
print(f'Your expenditure total is the same as your FSA contribution.')
elif difference < 0:
difference = (cont_value - exp_value) * -1
print(f'Your expenditure total is ${difference} over your FSA contribution for the year.')
elif difference > 0:
print(f'Your expenditure total is ${difference} under your FSA contribution for the year.')
here is the current code I am using
I would suggest deleting that last if/elif statement and rewrite it from scratch. Reset your mind and think, what is the calculation you need to do to find difference ?
subtracting the expenditure total from the contribution value
right, so how do you do that
difference = (cont_value - total)
there you go
Awesome
Two quick questions
- How would I make it where the cont_value has a max value of 3200
- How do I format it without decimals
Sorry for all the questions but you have been a great help @cursive crater
show current version
my_list = list()
cont_value = float(input('Enter your FSA contribution for the year as a whole number (max allowed is 3200): '))
exp_value = 1
while exp_value != 0:
exp_value = float(input('Enter your FSA expenditure for the year as a whole number (max allowed is 3200): '))
my_list.append(exp_value)
my_list.pop()
number_exp = len(my_list)
total = sum(my_list)
print(f'You have a count of {number_exp} FSA expenditures for the year.')
print(f'You have accumulated a total of ${total} for the year.')
difference = (cont_value - total)
if difference == 0:
print(f'Your expenditure total is the same as your FSA contribution.')
elif difference < 0:
difference = difference * -1
print(f'Your expenditure total is ${difference} over your FSA contribution for the year.')
elif difference > 0:
print(f'Your expenditure total is ${difference} under your FSA contribution for the year.')
you ask in your input for whole number, would you use float() to work with whole numbers?
try it out
yup
there are a few ways to do this, the easiest is to raise an error and stop the program when the user inputs a number too high
if <number> > 3200:
raise ValueError('Nah fam, too high.')
What other options are there
you could keep asking for new numbers until the number you are given is less than 3200
that would require few extra while loops
the insturction he gave was
Prompt the user to enter their total FSA contribution for the year. This value should be a whole number and stored in a variable. Within the prompt for this variable, state that the maximum allowed contributions is $3,200.
well he didn't say you have to enforce the 3200 rule
you could do something like
my_list = list()
cont_value = int(input('Enter your FSA contribution for the year as a whole number (max allowed is 3200): '))
while cont_value > 3200:
cont_value = int(input('Your FSA contribution exceeded the maximum allowed value of 3200. Try again: '))
exp_value = 1
while exp_value != 0:
exp_value = int(input('Enter your FSA expenditure for the year as a whole number (max allowed is 3200): '))
while exp_value > 3200:
exp_value = int(input('Your FSA expenditure exceeded the maximum allowed value of 3200. Try again: '))
my_list.append(exp_value)
my_list.pop()
number_exp = len(my_list)
total = sum(my_list)
print(f'You have a count of {number_exp} FSA expenditures for the year.')
print(f'You have accumulated a total of ${total} for the year.')
difference = (cont_value - total)
if difference == 0:
print(f'Your expenditure total is the same as your FSA contribution.')
elif difference < 0:
difference = difference * -1
print(f'Your expenditure total is ${difference} over your FSA contribution for the year.')
elif difference > 0:
print(f'Your expenditure total is ${difference} under your FSA contribution for the year.')
Thank you so much for the help
you saved my monday
no worries, gl with your other tasks
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.
๐ Need help on my code