Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #44328

Re: Guess the Number Repeat

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'modify': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Number': 0.09; 'def': 0.12; 'random': 0.14; '20)': 0.16; 'email addr:comcast.net': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.18; 'import': 0.22; 'header:User-Agent:1': 0.23; 'example.': 0.24; 'number)': 0.24; 'script': 0.25; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'invoke': 0.31; 'guess': 0.33; 'subject:the': 0.34; 'version': 0.36; 'too': 0.37; 'easily': 0.37; 'needed': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'flat': 0.60; 'break': 0.61; "you're": 0.61; 'within': 0.65; 'number:': 0.66; 'between': 0.67; 'guessed': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Guess the Number Repeat
Date Thu, 25 Apr 2013 11:22:24 +0200
Organization None
References <de1835ee-ef97-4d0c-99f8-f6cb348a252c@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084832f.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1053.1366881761.3114.python-list@python.org> (permalink)
Lines 72
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1366881761 news.xs4all.nl 15871 [2001:888:2000:d::a6]:39235
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:44328

Show key headers only | View raw


eschneider92@comcast.net wrote:

> How do I make the following program play the 'guess the number game'
> twice?
> 
> import random
> guessesTaken = 0
> print('Hello! What is your name?')
> myName = input()
> number = random.randint(1, 20)
> print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')
> while guessesTaken < 6:
>     print('Take a guess.')
>     guess = input()
>     guess = int(guess)
>     print('You have ' + str(5 - guessesTaken) + ' guesses left.')
>     guessesTaken = guessesTaken + 1
>     if guess < number:
>         print('Your guess is too low.')
>     if guess > number:
>         print('Your guess is too high.')
>     if guess == number:
>         break
> if guess == number:
>     guessesTaken = str(guessesTaken)
>     print('Good job, ' + myName + '! You guessed my number in ' +
>     guessesTaken + ' guesses!')
> if guess != number:
>     number = str(number)
>     print('Nope. The number I was thinking of was ' + number)


If you put everything needed to guess once into a function like in the 
following example. Once you have reorganised

#first version -- flat
guess = int(input("guess my number: "))
if guess == 42:
     print("congrats")
else:
     print("sorry, you're wrong")

into

#second version -- using a function
def guess_number():
    guess = int(input("guess my number: "))
    if guess == 42:
         print("congrats")
    else:
         print("sorry, you're wrong")

guess_number()

you can easily modify the script to invoke the guess_number() function 
inside a loop:

#third version -- calling the function from within a loop
def guess_number():
    guess = int(input("guess my number: "))
    if guess == 42:
         print("congrats")
    else:
         print("sorry, you're wrong")

for i in range(2):
    print("round", i+1)
    guess_number()



Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Guess the Number Repeat eschneider92@comcast.net - 2013-04-25 01:47 -0700
  Re: Guess the Number Repeat Peter Otten <__peter__@web.de> - 2013-04-25 11:22 +0200

csiph-web