#๐Ÿ”’ Need help on my code

150 messages ยท Page 1 of 1 (latest)

glacial marsh
#

I feel like I am very close to getting my desired result but there is a couple of issues

last driftBOT
#

@glacial marsh

Python help channel opened

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.

glacial marsh
#
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

cursive crater
# glacial marsh ```py cont_value = float(input('Enter your FSA contribution for the year as a wh...

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?

cursive crater
#

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?

glacial marsh
#

Yess

#

So I should remove that from the while loop as it is just 1 question

#

and get the value before the while loop?

cursive crater
#

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?

glacial marsh
#

Yes

#

I set it to 1 so it would start the loop

cursive crater
#

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

glacial marsh
#
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

cursive crater
#

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.
glacial marsh
#

But 1 time for the last statement

cursive crater
#

I'm talking about the example from your teacher

glacial marsh
#

1 time

#

all when the while loop ends

cursive crater
#

right, so that part will be outside of the while loop

glacial marsh
#

so would I move my if elif elif statement outside the while loop?

#

and just keep in exp_value

cursive crater
#

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

glacial marsh
#
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}')
cursive crater
#

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

glacial marsh
#

So i need to save them in a list in the while loop?

cursive crater
#

go for it

glacial marsh
#

Could you send me an example of how to store the user inputs in a listr

cursive crater
#

have you not been taught lists yet?

glacial marsh
#

nope

cursive crater
#

would you prefer trying a solution without them or do you want me to teach you?

glacial marsh
#

If you could teach me that would be helpful

cursive crater
# glacial marsh 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

glacial marsh
#

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

cursive crater
glacial marsh
#
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

cursive crater
#

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

glacial marsh
cursive crater
#

think of .append like + in 1 + 2

#

except in lists it just appends the number at the end, it doesn't do any calculation

glacial marsh
#
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

cursive crater
#

let's take a break and think about a few things in the code

#

my_list = list({exp_value}) do you need this?

glacial marsh
#

No

#

I already have my list stated at the top of the code

cursive crater
#

difference = '' do you need this?

glacial marsh
#

No

#

It gets the value in the while loop

cursive crater
#

how_many_elements = len(my_list) do you need to find out the length in the loop or after the loop is done?

glacial marsh
#

after the loop is done

cursive crater
#

cool, make those changes

glacial marsh
#

Ok after making the changes it is still off

#

It counts the last input of 0 as an input Im assuming

cursive crater
#

that's okay, we are not done yet

#

show the current code

glacial marsh
#
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}')
cursive crater
#

cool, so when you do 0 as an input, what happens in the code?

glacial marsh
#

it ends the while loop

cursive crater
#

anything else?

glacial marsh
#

it also sets one of the exp_values to zero

#

as it is the number based off user input

cursive crater
#

what happens first:

  1. exp_value gets added to my_list
  2. exp_value value is checked and while loop is stopped
glacial marsh
cursive crater
#

right, if exp_value = 0, is that what we want?

glacial marsh
#

No

cursive crater
#

what can we do to prevent it?

glacial marsh
#

subtract one from the list

#

like remove the last input

cursive crater
#

sure, write the code

#

oh I need to tell you how ๐Ÿ˜„

glacial marsh
#

yep

cursive crater
#

you can remove the last item in a list using .pop()

#

!e

my_list = [1,2,3]
my_list.pop()
print(my_list)
last driftBOT
glacial marsh
#

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

glacial marsh
cursive crater
#

nice! ๐Ÿ™‚

glacial marsh
#

I reread my code and it seems like it should work

cursive crater
#

what is not working as you would expect

glacial marsh
#

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

cursive crater
#

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 ?

glacial marsh
#

subtracting the expenditure total from the contribution value

cursive crater
#

right, so how do you do that

glacial marsh
#

difference = (cont_value - total)

cursive crater
#

there you go

glacial marsh
#

Awesome

#

Two quick questions

#
  1. How would I make it where the cont_value has a max value of 3200
#
  1. How do I format it without decimals
#

Sorry for all the questions but you have been a great help @cursive crater

cursive crater
#

show current version

glacial marsh
#
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.')
cursive crater
glacial marsh
#

No

#

would it be int?

cursive crater
#

try it out

glacial marsh
#

yup

cursive crater
glacial marsh
#

What other options are there

cursive crater
#

you could keep asking for new numbers until the number you are given is less than 3200

#

that would require few extra while loops

glacial marsh
#

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.

cursive crater
#

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.')
glacial marsh
#

you saved my monday

cursive crater
#

no worries, gl with your other tasks

last driftBOT
#
Python help channel closed

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