Problems with scripts

Joel Goldstick joel.goldstick at gmail.com
Mon Feb 13 11:55:50 EST 2017


On Mon, Feb 13, 2017 at 11:30 AM,  <lauren.sophia1998 at gmail.com> wrote:
> Hello! I have 2 python assignments that I just can't figure out. The first one returns the same thing no matter what I input and the second won't accept "done" to stop the program and return answers. Please help!
>
> 1)
> print("How old are you: 17, 18, 19, or 20?")
> answer = input("> ")
> if answer == 17 or 18 or 19 or 20:

You can't do if like you are trying to do.  First, you are comparing
to numbers, but you have a string for answer.  So, convert answer to
an int, and then look up 'if answer in (17,18, etc.):

>     print("Wow, you are old!")
> elif answer != 17 or 18 or 19 or 20:
>     if type(answer) is int or float:
>         print("You just can't follow drections, can you? Choose either 17, 18, 19, or 20.")
>         input("> ")
>     elif type(answer) is str:
>         print("That isn't even a number. Choose either 17, 18, 19, or 20.")
>         input("> ")
>
>
>
> 2)
> print("This program keeps track of the prices of items and how many items bought at that price. Enter 'done' to return the answers.")
> item_num = 1
> total_price = 0
> price = input("What is the price of item number " + str(item_num) + " ? ")
> total_items = 0
> how_many = input("How many items at that price? ")
> while price or how_many != "done":
>     total_price = int(total_price) + int(price)
>     total_items = int(total_items) + int(how_many)
>     item_num = item_num + 1
>     price = input("What is the price of item number " + str(item_num) + " ? ")
>     how_many = input("How many items at that price? ")
>
> if input == done:
>     print("The total price is $" + str(total_price) + ". And the number of items for that price is " + str(total_items) + ".")
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays



More information about the Python-list mailing list