[Tutor] cant get it to run right

Alan Gauld alan.gauld at yahoo.co.uk
Thu Mar 28 21:53:01 EDT 2019


On 28/03/2019 23:55, Sveum, Christian wrote:
> I am new and I have tried everything I can think I of. I want it to run like a converstion.

Its always good to be as specific as possible.
What exactly do you mean by "can't get it to run right"?
Does it crash? Do you get an error message?
If so send the message to us.

Does it give the "wrong answer" If so tell us what you
expected and what you got.

> print(" I am Bob")
> name = input("What is your name")
> print("Nice to meet you")
> age = input("How old are you?")
> print("I was not programed with a age")
> city = input("Where do you live?")

You save all these values but don't use them.

> temperature = float(input("What is the temperature?"))
> if temperature>=75:
>           print("WOW it is hot")
> elif temperature==75 or 74 or 73 or 72 or 71 or 70 or 69 or 68 or 67 or 66 or 65:
>           print(" That is nice")

Now this might be where you hit problems.
The code above is valid but doesn't do what I suspect
you think it does. Python sees it as:

elif (temperature==75) or (74 or 73 or 72 or 71
                   or 70 or 69 or 68 or 67 or 66 or 65):

And the expression (75 or 74 0r...) always evaluates to
the first non-zero value, so in this case it's 74

But we know temperature is not equal to 75 (from the if test)
so the elif expression always evaluates to 74 which is True
and so you get the printed message.

To do what you want you have several options.
 In your case the values form a range so you can test that:

elif 75 > temperature > 65:    # between 65 and 75
   print...

Or you can put your values in a list and use 'in':

elif temperature in [75,74,73,72,71,70,69,68,67,66,65]:
    print....

The range option is vest if your user can type in
non-whole numbers like 72.5 (as the use of float suggests)
The list is best if you have a set of distinct but non
consecutive values)


> if color=="Red" or "red" or "yellow" or "Yellow" or "Blue" 
            or "blue" or "orange" or "Orange" or "green"
            or "Green" or "purple" or "Purple":

Same here but the list approach is the only option here.
But see below...

> elif color=="white" or "White":

in this case the best approach is to convert the strings to upper or
lower case:

elif color.lower() == "white":

> elif color=="gray" or "Gray":
> elif color=="black" or "Black":

Same here


> while count<maxNumber:
>     if count==10:
>         print( "10 is my favorite number!")
>     print(count)
>     count=count+1

> input("Was one of these numbers your favorite number")
> if question=="yes":

I suspect you meant to assign the input() value to question?

HTH
-- 
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list