Re: python code

Top Page
Attachments:
Message as email
+ (text/plain)
+ (text/html)
+ (text/plain)
Delete this message
Reply to this message
Author: James Dugger
Date:  
To: Main PLUG discussion list
Subject: Re: python code
Hey Mike, Great job getting started with the book. Looks like you have a
lot of help so far too.

I would set the code up like this:

 print("Welcome!")
  guess = 0
  while guess != 5:
g = input("Guess the number: ")
    guess = int(g)
    if guess < 5:
        print("Guess higher.")
    elif guess > 5:
        print("Guess lower.")
    else:
        print("Correct.”)


Just set guess = 0 explicitly. Because you are setting the value there is
no need to then pass it through the first int() function. This is cleaner
because you are only using the variable "g" once.






On Mon, Apr 13, 2015 at 9:04 PM, Sesso <> wrote:

> If I remember correctly, int(g) makes the g an integer. So if you typed
> 9.6 , the int() would make it 9. If you know that the user will only input
> an integer, you probably won’t even need to use int().
>
> The things you are asking about it correct.
>
> Jason
>
>
>
> On Apr 13, 2015, at 8:37 PM, Michael Havens <> wrote:
>
> So here are the snippets I am concerned about:
>
>     g = 0
>     guess = int(g)
>     g = input("Guess the number: ")
>     guess = int(g)

>
> g = 0 is saying 'variable g is zero)
>
> guess = int(g) is saying that the variable guess is equal to variable g
> and that it is an integer
>
> g = input("<text>") is saying that variable g is now equal to what the use
> inputs as long as it is a number
>
> the 2nd guess = int(g)is there to put the variable in the while loop
>
> Is that correct?
>
> :-)~MIKE~(-:
>
> On Mon, Apr 13, 2015 at 7:16 PM, Todd Millecam <> wrote:
>
>> It's a bad variable name is all. It could be
>> supercalifragilisticexpialidocious = input
>> But, it's not really descriptive. A better name would probably be
>> raw_user_guess = input
>>
>> On Mon, Apr 13, 2015 at 7:30 PM, Michael Havens <> wrote:
>>
>>> Cool, that is sorta how I wrote it before though they haven't gone over
>>> elif yet. As a result of that I deleted 'g = input("Guess the number:
>>> ")' and it kept complaining about the variable not being defined. Question
>>> though:
>>> could we change the 'g' to some other letter...... never mind! I just
>>> tried it with 'w' and it works. What does that digit represent?
>>>
>>> :-)~MIKE~(-:
>>>
>>> On Mon, Apr 13, 2015 at 3:04 PM, Sesso <> wrote:
>>>
>>>> elif is what you want to use. Here try this. :)
>>>>
>>>>
>>>>
>>>> elif = else if
>>>>
>>>> I added the g=0 because the variable needed to be defined.
>>>> Same with guess.
>>>>
>>>>
>>>> #!/usr/bin/env python
>>>> print("Welcome.")
>>>> g=0
>>>> guess = int(g)
>>>> while guess != 5:
>>>>     g = input("Guess the number: ")
>>>>     guess = int(g)
>>>>     if guess < 5:
>>>>         print("Guess higher.")
>>>>     elif guess > 5:
>>>>         print("Guess lower.")
>>>>     else:
>>>>         print("Correct.”)

>>>>
>>>> jason
>>>>
>>>>
>>>>
>>>> On Apr 13, 2015, at 1:34 PM, Michael Havens <> wrote:
>>>>
>>>> Okay Jason, I did it your way. The resulting code being 2 lines
>>>> shorter. Unfortunately I still have the same problem as before (that being
>>>> if the guess is wrong it gives no suggestion as to higher/lower but rather
>>>> just asks you to guess again):
>>>>
>>>> python 1-30guess.py
>>>> Welcome.
>>>> Guess the number: 0
>>>> Guess the number: 0
>>>> Guess higher.
>>>> Guess the number: 9
>>>> Guess lower.
>>>> Guess the number: 8
>>>> Guess lower.
>>>> Guess the number: 7
>>>> Guess lower.
>>>> Guess the number: 6
>>>> Guess lower.
>>>> Guess the number: 5
>>>> Guess lower.
>>>> Correct.
>>>>
>>>> here is the code:
>>>>
>>>> #!/usr/bin/env python
>>>> print("Welcome.")
>>>> g = input("Guess the number: ")
>>>> guess = int(g)
>>>> while guess != 5:
>>>>     g = input("Guess the number: ")
>>>>     guess = int(g)
>>>>     if guess < 5:
>>>>         print("Guess higher.")
>>>>     else:
>>>>         print("Guess lower.")
>>>> print("Correct.")

>>>>
>>>> :-)~MIKE~(-:
>>>>
>>>> On Mon, Apr 13, 2015 at 12:48 PM, Sesso <> wrote:
>>>>
>>>>> You may be better off using if statements.
>>>>>
>>>>>
>>>>> If number is less than 5
>>>>>
>>>>> print guess higher
>>>>>
>>>>> else
>>>>>
>>>>> print guess lower
>>>>>
>>>>> Then wrap all of that in a while loop with a condition of while less
>>>>> than 5.
>>>>>
>>>>> Jason
>>>>>
>>>>>
>>>>>
>>>>> On Apr 13, 2015, at 12:44 PM, Michael Havens <> wrote:
>>>>>
>>>>> however it does give suggestions on subsequent guesses....
>>>>>
>>>>> :-)~MIKE~(-:
>>>>>
>>>>> On Mon, Apr 13, 2015 at 12:42 PM, Michael Havens <>
>>>>> wrote:
>>>>>
>>>>>> here is what I have (that sorta works):
>>>>>>
>>>>>> print("Welcome.")
>>>>>> g = input("Guess the number: ")
>>>>>> guess = int(g)
>>>>>> while guess < 5:
>>>>>>     g = input("Guess the number: ")
>>>>>>     guess = int(g)
>>>>>>     print("Guess higher.")
>>>>>> while guess > 5:
>>>>>>     g = input("Guess the number: ")
>>>>>>     guess = int(g)
>>>>>>     print ("Guess lower.")
>>>>>> print("Correct")

>>>>>>
>>>>>> What I mean by 'sorta' is if the first guess is not correct it does
>>>>>> not give a suggestion to guess lower or higher. Why is this?
>>>>>>
>>>>>> :-)~MIKE~(-:
>>>>>>
>>>>>> On Mon, Apr 13, 2015 at 12:32 PM, Michael Havens <>
>>>>>> wrote:
>>>>>>
>>>>>>> that makes sense... thanks!
>>>>>>>
>>>>>>> :-)~MIKE~(-:
>>>>>>>
>>>>>>> On Mon, Apr 13, 2015 at 12:31 PM, Sesso <> wrote:
>>>>>>>
>>>>>>>> The while needs to before input so that it will wait for input if
>>>>>>>> the variable is not equal to 5.
>>>>>>>>
>>>>>>>> Jason
>>>>>>>>
>>>>>>>>
>>>>>>>> On Apr 13, 2015, at 12:21 PM, Michael Havens <>
>>>>>>>> wrote:
>>>>>>>>
>>>>>>>> nope. that didn't fix it.
>>>>>>>>
>>>>>>>>
>>>>>>>> :-)~MIKE~(-:
>>>>>>>>
>>>>>>>> On Mon, Apr 13, 2015 at 11:46 AM, Amit Nepal <>
>>>>>>>> wrote:
>>>>>>>>
>>>>>>>>> You would probably want to wait and ask for the input inside the
>>>>>>>>> loop as well. What you seem to be doing is you ask for input and then you
>>>>>>>>> execute the loop not letting the user input the new number and thus running
>>>>>>>>> into an infinite loop.
>>>>>>>>>
>>>>>>>>> This might fix it, not tested though :)
>>>>>>>>>
>>>>>>>>> print("Welcome.")
>>>>>>>>> g = input("Guess the number: ")
>>>>>>>>> guess = int(g)
>>>>>>>>>
>>>>>>>>> while guess != 5
>>>>>>>>>     print("Guess again.")
>>>>>>>>>     guess = int(g)
>>>>>>>>>     print("Correct")

>>>>>>>>>
>>>>>>>>>
>>>>>>>>> Thanks
>>>>>>>>> Amit
>>>>>>>>> On 4/13/2015 11:01 AM, Michael Havens wrote:
>>>>>>>>>
>>>>>>>>>> print("Welcome.")
>>>>>>>>>> g = input("Guess the number: ")
>>>>>>>>>> guess = int(g)
>>>>>>>>>> while guess != 5:
>>>>>>>>>>     print("Guess again.")
>>>>>>>>>> print("Correct")

>>>>>>>>>>
>>>>>>>>>
>>>>>>>>> ---------------------------------------------------
>>>>>>>>> PLUG-discuss mailing list -
>>>>>>>>> To subscribe, unsubscribe, or to change your mail settings:
>>>>>>>>> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>>>>>>>>>
>>>>>>>>
>>>>>>>> ---------------------------------------------------
>>>>>>>> PLUG-discuss mailing list -
>>>>>>>> To subscribe, unsubscribe, or to change your mail settings:
>>>>>>>> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> ---------------------------------------------------
>>>>>>>> PLUG-discuss mailing list -
>>>>>>>> To subscribe, unsubscribe, or to change your mail settings:
>>>>>>>> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>> ---------------------------------------------------
>>>>> PLUG-discuss mailing list -
>>>>> To subscribe, unsubscribe, or to change your mail settings:
>>>>> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>>>>>
>>>>>
>>>>>
>>>>> ---------------------------------------------------
>>>>> PLUG-discuss mailing list -
>>>>> To subscribe, unsubscribe, or to change your mail settings:
>>>>> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>>>>>
>>>>
>>>> ---------------------------------------------------
>>>> PLUG-discuss mailing list -
>>>> To subscribe, unsubscribe, or to change your mail settings:
>>>> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>>>>
>>>>
>>>>
>>>> ---------------------------------------------------
>>>> PLUG-discuss mailing list -
>>>> To subscribe, unsubscribe, or to change your mail settings:
>>>> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>>>>
>>>
>>>
>>> ---------------------------------------------------
>>> PLUG-discuss mailing list -
>>> To subscribe, unsubscribe, or to change your mail settings:
>>> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>>>
>>
>>
>>
>> --
>> Todd Millecam
>>
>> ---------------------------------------------------
>> PLUG-discuss mailing list -
>> To subscribe, unsubscribe, or to change your mail settings:
>> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>>
>
> ---------------------------------------------------
> PLUG-discuss mailing list -
> To subscribe, unsubscribe, or to change your mail settings:
> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>
>
>
> ---------------------------------------------------
> PLUG-discuss mailing list -
> To subscribe, unsubscribe, or to change your mail settings:
> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>




--
James

*Linkedin <http://www.linkedin.com/pub/james-h-dugger/15/64b/74a/>*
---------------------------------------------------
PLUG-discuss mailing list -
To subscribe, unsubscribe, or to change your mail settings:
http://lists.phxlinux.org/mailman/listinfo/plug-discuss