Re: Problems with scripts

2017-02-13 Thread Steve D'Aprano
On Tue, 14 Feb 2017 03:30 am, lauren.sophia1...@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!

Hi Lauren,

Best to ask one question per post, unless they are very closely related. You
might like to resend your second question in a new message, with a
different SUBJECT line.



> print("How old are you: 17, 18, 19, or 20?")
> answer = input("> ")

After this line, "answer" is a string. Even if it looks like a number, it is
still a string. Have you learned about the differences between strings and
integers yet?

How do you think you might change answer to an int, or a float, if and only
if it can, but leave it as a string if not?


> if answer == 17 or 18 or 19 or 20:
> print("Wow, you are old!")


Nice try, but not quite! Python code is very close to English, but not
*that* close. In English, you can say:

if the answer equals 17 or 18 or 19 or 20

but Python doesn't understand that. You can write this instead:

if answer == 17 or answer == 18 or answer == 19 or answer == 20:

but perhaps an easier way is:

if answer in (17, 18, 19, 20):

(But remember, at the moment answer is still a string. Strings are never
equal to ints, even if they look the same.)


> elif answer != 17 or 18 or 19 or 20:

There's no need to repeat the test, just say

else:

instead.


> 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("> ")




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Problems with scripts

2017-02-13 Thread Wildman via Python-list
On Mon, 13 Feb 2017 10:08:11 -0800, Lauren Fugate wrote:

> So I tried both of these and they didn't change anything, the python shell 
> printed the same things...

The first assignment is overly complicated.  The extra input functions are
useless.  There is no loopback to check the input.  Also, input returns
a string not an int or float.  Try this:

x = ["17","18","19","20"]
answer = None
print("How old are you: 17, 18, 19, or 20?")
while answer not in x:
answer = input("> ")
if answer in x:
print("Wow, you are old!")
else:
print("You just can't follow drections, can you? Choose either 17, 18, 
19, or 20.")

Keep in mind that the above code does not give you a way out.
One of the expected numbers must be entered.

I'll leave the second one for you to figure out.

-- 
 GNU/Linux user #557453
The cow died so I don't need your bull!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Problems with scripts

2017-02-13 Thread alister
On Mon, 13 Feb 2017 10:08:11 -0800, Lauren Fugate wrote:

> So I tried both of these and they didn't change anything, the python
> shell printed the same things...

No answers (answering you homework for you will not teach you anything 
useful) but some hints to help you think about the problem

1) why check for each option explicitly, why not simply check if the 
input is between acceptable limits.

2) instead of checking for valid inputs, why not react to the invalid ones

3) what is the error you are getting when you try to change your input to 
an int, can you TRY* to do anything with this error?

* this is a very big hint

-- 
YOW!!  Now I understand advanced MICROBIOLOGY and th' new TAX REFORM 
laws!!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Problems with scripts

2017-02-13 Thread Lauren Fugate
So I tried both of these and they didn't change anything, the python shell 
printed the same things...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Problems with scripts

2017-02-13 Thread Lauren Fugate
So I changed my code to this:

print("How old are you: 17, 18, 19, or 20?") 
answer = input("> ") 
if int(answer) == 17 or 18 or 19 or 20:
print("Wow, you are old!") 
else:
print("You just can't follow drections, can you? Choose either 17, 18, 19, 
or 20.")
input("> ") 

and now if I input any of the preferred answers it will print "Wow, you are 
old!" like it should. But if I enter a word, like "hi", it will return an 
error. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Problems with scripts

2017-02-13 Thread Wildman via Python-list
On Mon, 13 Feb 2017 08:30:32 -0800, lauren.sophia1998 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:
> 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("> ")

Incorrect use of 'or'.

if answer == 17 or answer == 18 or answer == 19 or answer == 20:

elif answer != 17 and answer != 18 and answer != 19 and answer != 20:

if type(answer) is int or type(answer) is float:

> 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) + ".")

Same here:

while price != "done" and how_many != "done":

-- 
 GNU/Linux user #557453
May the Source be with you.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Problems with scripts

2017-02-13 Thread Joel Goldstick
On Mon, Feb 13, 2017 at 11:30 AM,   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
-- 
https://mail.python.org/mailman/listinfo/python-list


Problems with scripts

2017-02-13 Thread lauren . sophia1998
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:
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