Python Forum
Newbie needs help with a simple program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie needs help with a simple program
#1
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
Reply
#2
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
Reply
#3
(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
Reply
#4
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')
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Suggestions for a simple data analysis program t4keheart 0 473 Mar-08-2021, 03:45 PM
Last Post: t4keheart
  logging in simple program Inkanus 1 504 Dec-18-2020, 02:36 PM
Last Post: snippsat
  I am a newbie.Help me with this simple piece of code feynarun 3 1,132 Jan-08-2020, 12:40 PM
Last Post: perfringo
  Python Program to Make a Simple Calculator jack_sparrow007 2 7,251 Oct-19-2018, 08:32 AM
Last Post: volcano63
  help with simple program juanb007 2 1,638 Dec-07-2017, 02:15 PM
Last Post: j.crater
  Some Confusing Program Errors (Newbie stuff) WildPictus 1 1,643 Sep-03-2017, 05:00 PM
Last Post: hbknjr
  Newbie help with simple script written for v3.3 and used on vs2.7, please? PaulMorrey 4 3,232 Dec-09-2016, 11:06 AM
Last Post: Kebap
  a 'simple' program, hard as .... to understand meems 3 3,240 Dec-04-2016, 10:59 PM
Last Post: meems
  Cant Get Powershell to run a python program from notepad++. Newbie help! Pythonerous 4 5,833 Oct-10-2016, 07:16 PM
Last Post: Pythonerous

Forum Jump:

User Panel Messages

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