Python Forum
Can't figure out how to use these variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't figure out how to use these variables
#1
Hello so I am new to python, I only have experience in C#. This is on my assignment's I'm working on and I'm very confused. I am suppose to make it so when people use this program its like an order system for apples, cabbage, and bananas. I have to figure out how to make it so when they start it they are asked to input an item they would like so if they type apple it'll say okay how many and this is the price and then give me a total for that then move on to bananas, cabbages etc. I'm having trouble because I can't figure out how to use the item_total variable to give me the line total for each item to then give me the order_total at the bottom. Here is the code I've written so far, zero errors but not want I want to display I guess. Thanks!

#Define variables
item_total = float(0)
order_total = float(0)
#Welcome
print("Hello welcome to the store")

# Input
item1_apple = str("apple")
item1_qty = int(input("please enter number of apples = "))
item1_price = float(0.6)

item2_banana = str("banana")
item2_qty = int(input("please enter number of bananas = "))
item2_price = float(1.5)

item3_cabbage = str("cabbage")
item3_qty = int(input("please enter number of cabbage = "))
item3_price = int(3)




# process
item_total = (item1_apple + item2_banana + item3_cabbage)
apple_total = (item1_qty * item1_price)
banana_total = (item2_qty * item2_price)
cabbage_total = (item3_qty * item3_price)
                
order_total = apple_total + banana_total + cabbage_total
order_total = 'Order total: $'+str(order_total)
print()
print('-' * 30)
print("order total for {} are {}" .format,(order_total))
print('-' * 30)
Reply
#2
Hello and welcome to Python and the forums!
First, you don't need to tell Python what data type a variable should be, it figures that out when it is assigned. So you can leave out float before 0.6, string before "apple" etc. You do however need it when you take input from a user and want to use that input as something other than a string. Your code handles that properly already.

item_total variable is a concatenation of 3 strings, I doubt you want that. To me it seems that apple_total, banana_total and cabbage_total are doing what you expect correctly. And after that you get the total sum in order_total. Here is one way of doing it:

# Define variables
item_total = 0
order_total = 0

# Welcome
print("Hello welcome to the store")

# Input
item1_apple = "apple"
item1_qty = int(input("please enter number of apples = "))
item1_price = 0.6

item2_banana = "banana"
item2_qty = int(input("please enter number of bananas = "))
item2_price = 1.5

item3_cabbage = "cabbage"
item3_qty = int(input("please enter number of cabbage = "))
item3_price = 3

# process
item_total = (item1_apple + item2_banana + item3_cabbage)
apple_total = (item1_qty * item1_price)
banana_total = (item2_qty * item2_price)
cabbage_total = (item3_qty * item3_price)

order_total = apple_total + banana_total + cabbage_total
print('Order total: ${}'.forma# Define variables
item_total = 0
order_total = 0

# Welcome
print("Hello welcome to the store")

# Input
item1_apple = "apple"
item1_qty = int(input("please enter number of apples = "))
item1_price = 0.6

item2_banana = "banana"
item2_qty = int(input("please enter number of bananas = "))
item2_price = 1.5

item3_cabbage = "cabbage"
item3_qty = int(input("please enter number of cabbage = "))
item3_price = 3

# process
apple_total = (item1_qty * item1_price)
banana_total = (item2_qty * item2_price)
cabbage_total = (item3_qty * item3_price)


order_total = apple_total + banana_total + cabbage_total
print('Order total: ${}'.format(order_total))
print('-' * 30)
print("order total for {} are ${:.2f}".format(item1_apple, apple_total))
print('-' * 30)
print("order total for {} are ${:.2f}".format(item2_banana, banana_total))
print('-' * 30)
print("order total for {} are ${:.2f}".format(item3_cabbage, cabbage_total))
print('-' * 30)
The code has a lot of relations between variables (for example item and its price and quantity). So it could be greatly improved by using lists and dictionaries. I suggest you to dig into the topic and modify your code with using them. If you will have questions/problems feel free to post back.
Reply
#3
Ah thank you! That was very helpful I think that's what will do! The assignment did instruct I use the item_total variable to get the line total for the first item then the other two individually as well. Then use those to get the order_total but it's like you said the item_total would be a concatenation of the three. Thanks!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020