'Why does my Python code not output anything? [closed]
I wanted to create a Python code where it's a cashier asking questions and putting the inputted products in the dictionary but it doesn't output anything.
def enterProducts():
buyingData = {}
enterDetails = True
while enterDetails:
details = input("Press A to add product and Q to quit:")
if details == 'A':
product = input("Enter product:")
quantity = int(input("Enter quantity:"))
buyingData.updata({product: quantity})
elif details == 'Q':
enterDetails = False
else:
print('Please select a correct option')
return buyingData
Solution 1:[1]
Change .updata
to .update
and (I don't know the context) I've added if __name__ == "__main__": ...
Working example:
def enter_products():
buying_data = {}
enter_details = True
while enter_details:
details = input("Press A to add product and Q to quit:")
if details == 'A':
product = input("Enter product:")
quantity = int(input("Enter quantity:"))
buying_data.update({product: quantity}) #Change
elif details == 'Q':
enter_details = False
else:
print('Please select a correct option')
print(buying_data)
#return buying_data
if __name__ == "__main__":
enter_products()
Solution 2:[2]
Try this
def enterProducts():
buyingData = {}
enterDetails = True
while enterDetails:
details = input("Press A to add product and Q to quit:")
if details == 'A':
product = input("Enter product:")
try:
quantity = int(input("Enter quantity:"))
buyingData.update({product: quantity})
except:
print("It should be a integer")
elif details == 'Q':
enterDetails = False
return buyingData
else:
print('Please select a correct option')
l = enterProducts()
print(l)
Solution 3:[3]
You can add and updates dicts in python rather simply with buyingData[product] = quantity
.
If you want to insist using the update()
, make sure you are writing it correctly - your code has a typo. The correct working example would be:
def enterProducts():
buyingData = {}
enterDetails = True
while enterDetails:
details = input("Press A to add product and Q to quit:")
if details == 'A':
product = input("Enter product:")
quantity = int(input("Enter quantity:"))
buyingData.update({product: quantity})
elif details == 'Q':
enterDetails = False
return buyingData
else:
print('Please select a correct option')
product_quantities = enterProducts()
print(product_quantities)
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 | mariusr |
Solution 2 | Thierno Amadou Sow |
Solution 3 |