#๐Ÿ”’ Need help on fixing my code asap

73 messages ยท Page 1 of 1 (latest)

keen heronBOT
#

@mossy obsidian

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.

hushed saffron
#

??

mossy obsidian
#
contribution_value = float(input('Enter your FSA contribution for the year as a whole number (max allowed is 3200): '))

expenditure_value = float(input('Enter your FSA expenditure for the year as a whole number (max allowed is 3200): '))

difference = contribution_value - expenditure_value

while contribution_value and expenditure_value != 0:
    if difference < 0:
        print(f'The difference is {difference}')
    elif difference > 0:
        difference * -1
        print(f'{difference}')
    elif difference == 0:
        print(f'difference is same') 
        

print(f'{contribution_value} and {expenditure_value}')
keen heronBOT
#

Hey @mossy obsidian!

It looks like you incorrectly specified a language for your code block.

Make sure you put your code on a new line following py. There must not be any spaces after py.

Here is an example of how it should look:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
hushed saffron
#

what's the issue ?

mossy obsidian
#
contribution_value = float(input('Enter your FSA contribution for the year as a whole number (max allowed is 3200): '))

expenditure_value = float(input('Enter your FSA expenditure for the year as a whole number (max allowed is 3200): '))

difference = contribution_value - expenditure_value

while contribution_value and expenditure_value != 0:
    if difference < 0:
        print(f'The difference is {difference}')
    elif difference > 0:
        difference * -1
        print(f'{difference}')
    elif difference == 0:
        print(f'difference is same') 
        

#

Ok so

ruby badge
#

while contribution_value and expenditure_value != 0:

#

!or

keen heronBOT
#
The or-gotcha

When checking if something is equal to one thing or another, you might think that this is possible:

# Incorrect...
if favorite_fruit == 'grapefruit' or 'lemon':
    print("That's a weird favorite fruit to have.")

While this makes sense in English, it may not behave the way you would expect. In Python, you should have complete instructions on both sides of the logical operator.

So, if you want to check if something is equal to one thing or another, there are two common ways:

# Like this...
if favorite_fruit == 'grapefruit' or favorite_fruit == 'lemon':
    print("That's a weird favorite fruit to have.")

# ...or like this.
if favorite_fruit in ('grapefruit', 'lemon'):
    print("That's a weird favorite fruit to have.")
hushed saffron
#

wait

mossy obsidian
#

I am trying to have it repeatedly ask the contrbution value until the user inputs 0

hushed saffron
#

got it

#

while contribution_value != 0 and expenditure_value != 0:

#

try this

mossy obsidian
#

No

#

it still asks what time

mossy obsidian
hushed saffron
#

ok let me have look

#

for which variable

#

?

mossy obsidian
#

that last line of code was useless

#

both

hushed saffron
#

ok

mossy obsidian
#

contrbution value and expenditure value

#

and I also need it to display the total number of inputs for expenditure

#

im so confused ngl

mossy obsidian
#

heres an example

#

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.

hushed saffron
#

got it

#

give me a min

mossy obsidian
#

alright

hushed saffron
#

def ask_for_inputs():
cont = int(input("Enter your FSA contribution for the year as a whole number (max allowed is 3200): "))
expen = int(input("Enter your FSA expenditure as a whole number (enter 0 to finish): "))

return [cont, expen]

initial_cont = 1
initial_expen = 1

vals = []
while initial_cont != 0 and initial_expen != 0:

inputs = ask_for_inputs()
initial_cont = inputs[0]    
initial_expen = inputs[1]

vals.append(initial_expen)

x = 0
for values in vals:
x += values

print(f"You have a count of {len(vals)} expenditures of the year.")
print(f"You have accumulated a total of ${x} for the year")

keen heronBOT
#

Hey @hushed saffron!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
hushed saffron
#
def ask_for_inputs():
    cont = int(input("Enter your FSA contribution for the year as a whole number (max allowed is 3200): "))
    expen = int(input("Enter your FSA expenditure as a whole number (enter 0 to finish): "))

    return [cont, expen]

initial_cont = 1
initial_expen = 1


vals = []
while initial_cont != 0 and initial_expen != 0:

    inputs = ask_for_inputs()
    initial_cont = inputs[0]    
    initial_expen = inputs[1]

    vals.append(initial_expen)

x = 0
for values in vals:
    x += values

print(f"You have a count of {len(vals)} expenditures of the year.")
print(f"You have accumulated a total of ${x} for the year")
    

ruby badge
#

Rather than complete OP's task for them please consider teaching.
Be aware OP has >1 channels open atm asking for assistance on different exercises.

hushed saffron
mossy obsidian
#

Running this code it does not repeat the question

#

rather it takes the input and prompts the user with the other question

mossy obsidian
#

I see now

#

How would I be able to change the output to be

#

Your expenditure total is $1000 under your FSA contribution for the year

#

if its under

#

Your expenditure total is $1000 overt your FSA contribution for the year

#

and same

hushed saffron
#

now please learn the logic and try to build a diagram in your head

Step 1: Create a function to take input, making it function would help us to avoid repetition of code.
Step 2: Define initial non-zero values to execute the loop
Step 3: Define a list outside the loop to keep a track of total contribution and the total amount.
Step 4: Once loop is over get the length to get the number of contributions
Step 5: iterate over the list and add the values and print appropriate statements.

ruby badge
hushed saffron
mossy obsidian
#

It doesnt do it right tho

ruby badge
#

I asked if it was your code.
Because if so, then you already have the required knowledge to do what you are now asking.
If you don't, please make some attempt and let us know where you are going wrong so that you can be assisted in a way other than just producing code for you. We aren't here for that.

mossy obsidian
#

when I enter my codce

hushed saffron
#

@ruby badge Be aware OP has >1 channels open atm asking for assistance on different exercises. i didn't get it

ruby badge
#

My suspicion is OP isn't doing this work - they're using our server to get exercises completed with little effort.
Our code of conduct pretty well covers how to ask questions, how to help, etc. And it's just generally bad form.

ruby badge
#

If I'm wrong I'm happy to apologise - but let's see some back and forth so that OP can be shown and helped in a way that is beneficial.

mossy obsidian
#

im just trying to get help its not like I havent been trying for the last 2 hours ๐Ÿ˜ญ

hushed saffron
hushed saffron
ruby badge
#

@mossy obsidian please show the code you're attempting as well as the traceback.

mossy obsidian
#

What does that do in the program?

#

Im just curious

#

Same with

#

x = 0
for values in vals:
x += values

#

the rest I understand pretty well

hushed saffron
#

basically setting this value to a non-zero int which helps start the loop

hushed saffron
#

so we take each value from the loop and add it to x (total)

keen heronBOT
#
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.