python code

Todd Millecam tyggna at gmail.com
Mon Apr 13 13:14:15 MST 2015


Here's mine, you're pretty much right on track now.  I thought I'd include
this just to introduce a few new concepts and help you think a little more
like a computer.

#!/usr/bin/env python
#This first line is really nifty, and all Linux users should be familiar
with this.
# If you pass this file into python, the first line gets ignored, but if
you pass it into bash or sh, then a # and a ! are called a magic byte, or
whiz-bang for short
# They tell bash to open the rest of this file using the program and
arguments passed on the first line.
# So this line changes my python script into a bash program, and if I had
saved this as /tmp/myguess.py, I could then, from a bash prompt type in
/tmp/myguess.py to run it

from random import randint
# Right after the first line, it's polite to import all your libraries and
functions right at the top.  This particular one says I want, from the
"random" library to get a function called "randint"
# randint returns a random integer in a specified range
# The rest of the file is my actual program

answer = randint(0,10)
guess = int() # this is called an uninitialized integer.  It has the space
of ram allocated, but no value placed in it yet.  These tend to be about
100x faster than initialized data types
while guess is not answer: #same thing as guess != answer
    if guess is int(): # I only want something printed after they've guessed
        pass
    else:
        print("Wrong.")
    #Since this part is at the same indentation level as the if and else,
it will always get executed
    g = input("Guess my number: ")
    guess = int(g)
print("Correct!")




On Mon, Apr 13, 2015 at 1:48 PM, Sesso <sesso at djsesso.com> 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 <bmike1 at gmail.com> wrote:
>
> however it does give suggestions on subsequent guesses....
>
> :-)~MIKE~(-:
>
> On Mon, Apr 13, 2015 at 12:42 PM, Michael Havens <bmike1 at gmail.com> 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 <bmike1 at gmail.com>
>> wrote:
>>
>>> that makes sense... thanks!
>>>
>>> :-)~MIKE~(-:
>>>
>>> On Mon, Apr 13, 2015 at 12:31 PM, Sesso <sesso at djsesso.com> 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 <bmike1 at gmail.com> wrote:
>>>>
>>>> nope. that didn't fix it.
>>>>
>>>>
>>>> :-)~MIKE~(-:
>>>>
>>>> On Mon, Apr 13, 2015 at 11:46 AM, Amit Nepal <amit at amitnepal.com>
>>>> 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 - PLUG-discuss at lists.phxlinux.org
>>>>> To subscribe, unsubscribe, or to change your mail settings:
>>>>> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>>>>>
>>>>
>>>> ---------------------------------------------------
>>>> PLUG-discuss mailing list - PLUG-discuss at lists.phxlinux.org
>>>> To subscribe, unsubscribe, or to change your mail settings:
>>>> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>>>>
>>>>
>>>>
>>>> ---------------------------------------------------
>>>> PLUG-discuss mailing list - PLUG-discuss at lists.phxlinux.org
>>>> To subscribe, unsubscribe, or to change your mail settings:
>>>> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>>>>
>>>
>>>
>>
> ---------------------------------------------------
> PLUG-discuss mailing list - PLUG-discuss at lists.phxlinux.org
> To subscribe, unsubscribe, or to change your mail settings:
> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>
>
>
> ---------------------------------------------------
> PLUG-discuss mailing list - PLUG-discuss at lists.phxlinux.org
> To subscribe, unsubscribe, or to change your mail settings:
> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>



-- 
Todd Millecam
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.phxlinux.org/pipermail/plug-discuss/attachments/20150413/415fe0ae/attachment.html>


More information about the PLUG-discuss mailing list