Python Forum
Newbie needs help with a simple program - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Newbie needs help with a simple program (/thread-23744.html)



Newbie needs help with a simple program - feynarun - Jan-15-2020

I am trying to follow a python youtube channel by Mosh Hamedani and I tried to write a simple piece of code after seeing his program. My basics are not that good. My program did not work. I was trying to write a program in which if the user enters the name of a vegetable the program gives the price of the vegetable. The program did not work. Please help me.

vegetable = input("Enter the names of vegetables: ")
price_mapping = {
    "onion": "50",
    "carrot": "20",
    "tomato": "10"
}

output = ""
print("These are the prices of vegetables")
for ch in vegetable:

    output += price_mapping.get(ch) + " "

print(output)
This was the output

Output:
Enter the names of vegetables: onion carrot tomato Traceback (most recent call last): File "C:/Users/user/PycharmProjects/untitled/Test.py", line 12, in <module> output += price_mapping.get(ch) + " " TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' These are the prices of vegetables Process finished with exit code 1



RE: Newbie needs help with a simple program - Larz60+ - Jan-15-2020

this code seems dysfunctional.
what is the intent of this exercise?

please provide a URL of the exercise. also, if a video, the time location of the exercise within the video

I would also like to point out a couple of 'tried and true' tutorials for python:
How to Think Like a Computer Scientist: Interactive Edition
Python3 Tutorial


RE: Newbie needs help with a simple program - feynarun - Jan-15-2020

(Jan-15-2020, 01:01 PM)Larz60+ Wrote: this code seems dysfunctional.
what is the intent of this exercise?

please provide a URL of the exercise. also, if a video, the time location of the exercise within the video

I would also like to point out a couple of 'tried and true' tutorials for python:
How to Think Like a Computer Scientist: Interactive Edition
Python3 Tutorial


I am just trying to take the names of vegetables as input and give their prices out as the output.
Please check out this link.Mosh Hamedani Tutorial


RE: Newbie needs help with a simple program - DeaD_EyE - Jan-15-2020

price_mapping = {
    "onion": 50,
    "carrot": 20,
    "tomato": 10
}
# I changed the mapping. The values are now integers.
# Are the prices in the smallest currency unit?
# If you calculate with prices, you should use integer or decimal
# Otherwise you get some inaccuracy
# A float can not represent all decimal values. This is impossible.
# summing up many floats will produce a growing inaccuracy.
# summing up many integers has no error


# Print the price list 
print("These are the prices of vegetables")
for name, price in price_mapping.items():
    print(name, price)
    # formatting is your task
    # and you should look what dicts are.
    # they have methods like keys(), values(), items()


vegetables = []
while True:
    vegetable = input("Enter the name of vegetable (empty to sum up): ").strip().lower()
    # the strip() method removes white space
    # the lower() method convert everything into lower case
    # this is less error prune, if the user for example use upper case letters
    # or whitespaces before or after the word.
    if not vegetable:
        # this is the break condition
        # if the string vegetable is empty
        # the loop stops and the code after the
        # loop is executed
        break
    if vegetable not in price_mapping:
        print('is not in price list')
        continue # ask again the question
    # this line is reached, if vegetable is not an empty string and
    # if vegetable is in price list
    vegetables.append(price_mapping[vegetable])
    # look what lists are. append puts the items to the end of a list


# now you can just sum the floats
result = sum(vegetables)
# the function sum takes any iterables and applies + for all elements.
print('You have to pay', result, 'bananas')
# Or you could use string formatting
# print(f'You have to pay {result} bananas')