Question about Automate The Boring Stuff Chapter 3 - python

I am on chapter three of Automate The Boring Stuff with Python. For exercise guessTheNumber.py I am unclear as to how "guessesTaken" was defined and how it is being incremented.
https://automatetheboringstuff.com/chapter3/
How is this program:
1. Defining the guessesTaken variable
2. increasing the value of guessesTaken for each guess
Thank you,
# This is a guess the number game.
import random
secretNumber = random.randint(1, 20)
print('I am thinking of a number between 1 and 20.')
# Ask the player to guess 6 times.
for guessesTaken in range(1, 7):
print('Take a guess.')
guess = int(input())
if guess < secretNumber:
print('Your guess is too low.')
elif guess > secretNumber:
print('Your guess is too high.')
else:
break # this condition is the correct guess!
if guess == secretNumber:
print('Good job! You guessed my number in ' + str(guessesTaken) +' guesses!')
else:
print('Nope. The number I was thinking of was ' + str(secretNumber))

It is used as a loop counter, it was defined in:
for guessesTaken in range(1, 7):
And is being incremented in every iteration of the for loop. So if the loop counter reaches 3, that means the loop ran (didn't break) three times, and so the user had to guess three times.

Related

How do I print a distinct response to the first try as opposed to the following five guesses? (Noob level number guessing game)

Just starting out here and was hoping someone could help me with an issue I'm having. Haven't been able to find any clear answers online, likely because this is such an early level exercise.
Basically I want to have the program print a different response after the first incorrect answer. For example... the first prompt is "Take a guess!" but after that I would like it to say... "Take another guess!"
Could anyone shed some light on this for me? Thanks in advance.
# Number guessing game.
import random
from time import sleep
secretNumber = random.randint(1, 20)
print('I am thinking of a number between 1 and 20. You have five chances to guess correctly.')
sleep(1)
# Ask the player to guess six times.
for guessesTaken in range(1, 6):
print('Take a guess!')
if guessesTaken > 1:
print('Take another guess!')
guess = int(input())
if guess < secretNumber:
print('Your guess is too low.')
elif guess > secretNumber:
print('Your guess is too high.')
else:
break #this is the correct guess!
if guess == secretNumber:
print('Good job! You guessed my number in ' + str(guessesTaken) + ' guesses!')
else:
print('Nope. The number I was thinking of was ' + str(secretNumber) + '.')
Instead of using print to show the prompt, add it to the input function call. Within that you can use a ternary-style expression. Here's an example:
import random
NGUESSES = 6
RANGE = 1, 20
computer = random.randint(*RANGE)
for guess in range(NGUESSES):
if (user := int(input('Take a guess: ' if guess == 0 else 'Try again: '))) == computer:
print(f'Good job! You guessed correctly in {guess+1} guesses')
break
print('Too low' if user < computer else 'Too high')
else:
print(f'Nope. The number I was thinking of was {computer}')

guessesTaken variable and the for statement

I just started learning programming with the book: Invent your own computer games with Python.
Here is the code that I am going to refer to. (Python 3.4)
# This is a guess the number game.
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.')
for guessesTaken in range(6):
print('Take a guess.')
guess = input()
guess = int(guess)
if guess < number-1:
print('Your guess is too low.')
if guess > number+1:
print('Your guess is too high.')
if guess == number + 1:
print('Close bruh!')
if guess == number - 1:
print('Almost!')
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken + 1)
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 + '.')
The book created a variable guessesTaken = 0. But if you run the program it doesn't matter what value you assign to guessesTaken at the beginning. It is as if Python forgets about it when the execution reaches the for statement. I can just as well omit the guessesTaken variable.
If it is true that Python forgets about the assignment statement for the variable, guessesTaken, then Python must use the guessesTaken variable next to the for statement to store the amount of loops that have been completed so that you can print it out for the user.
But the program prints the amount of guessesTaken + 1, so it means that Python counts the first loop as 0.
If all this is true, is it then always so that the variable next to a for statement is going to be the place where Python stores the amount of loops taken ? (as an integer)
Sorry, I started to sound like a Boolean data type :-)
You are correct, the original assignment of guessTaken is over written by guessTaken the for loop definition.
The variable in the for loop (in your case guessTaken) takes the values of the elements in the iterator itis looping over (in your case range(6))
In your case, range(6) returns the iterator [0,1,2,3,4,5] so guessTaken is 0, then 1, then 2 and so on up to 5.

Is there a way to change the output of the strings based on the conditions of the loop?

I hope I worded the question right. So I'm going through a Python textbook. (automate the boring stuff). In it, I am supposed to create a guess the number game in which the player has 6 chances to pick the correct number.
However, I would like to make a couple of changes that I'm struggling with.
I want the output to show the exact amount of chances left (6 chances, 5 chances, 4, chances ... 0). I think i may have solved this problem, but I believe you all may have a better way.
when the player is down to 1 chance, i would like the text to change from 'chances left' to 'chance left'.
# This is a guess the number game.
import random
secretNumber = random.randint(1, 20)
print('I am thinking of a number between 1 and 20.')
# Ask the player to guess 6 times.
for guessTaken in range(7, 1, -1):
print('Take a guess. You only have ' + str(guessTaken - 1) + ' chances left... ')
guess = int(input())
if guess < secretNumber:
print('Your guess is too low. ')
elif guess > secretNumber:
print('Your guess is too high. ')
else:
break # This condition is the correct guess
if guess == secretNumber:
print('Good job! you guessed my number i ' + str(guessTaken) + ' guesses!')
else:
print('Nope. The number I was thinking of was ' + str(secretNumber))
Change your range to range(6, 0, -1) so you don't need to subtract 1 in the message.
Use an if statement.
for guessTaken in range(6, 0, -1):
suffix = "s" if guessTaken > 1 else ""
print('Take a guess. You only have ' + str(guessTaken) + ' chance' + suffix + ' left... ')
You're also printing the wrong number in the success message. guessTaken is the number of guesses they have left, not the number of guesses they used. You need to change that to 7 - guessTaken.
Now may be a good time to learn about functions, if you haven't already. A simple function to do this is this:
def chances_remaining_message(num_chances):
if num_chances == 1:
return 'Take a guess. You only have 1 chance left...'
else:
return 'Take a guess. You only have ' + str(num_chances) + ' chances left...'
Which you would then call here:
# Ask the player to guess 6 times.
for guessTaken in range(7, 1, -1):
print(chances_remaining_message(guessTaken - 1))
guess = int(input())

Stuck in a while loop while using if statement

I'm new to python and I'm trying to make a simple Guess the number game, and I'm stuck in an if statement in a while loop. here is the code.
I'm experiencing it at the Your guess it too high and the low one. I tried breaking it, but it simply stops the whole things
def guess_the_number():
number = random.randrange(20)
guessesMade = 0
print('Take a guess')
guess = input()
guess = int(guess)
while guessesMade < 6:
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:
print'You got it in', guessesMade, 'guess(es)! Congratulaions!'
else:
print'I\'m sorry, the number was', number
You never increment guessesMade so guessesMade < 6 will always be True. You need to modify this value within your loop. You also need to move your prompt for user input into the loop
while guessesMade < 6:
guess = int(input('Take a guess'))
if guess < number:
print('Your guess is too low.')
guessesMade += 1
elif guess > number:
print('Your guess is too high.')
guessesMade += 1
else:
break

Number guessing game

import random
def guess_number():
numb = random.randrange (10) +1
guessestaken = 0
guess = input("whats your number")
while (guess != numb):
if (guess > numb):
print "too low"
elif(guess < numb):
print "too high"
else:
input("whats your next numb")
tries += 1
I am making a number guessing game with range 1 to 10 and I need help on getting the loop to stop. when I guess the number it keeps going
Here's a working example of what you're trying to do:
import random
guessesTaken = 0
number = random.randint(1, 20)
print('I am thinking of a number between 1 and 20.')
while guessesTaken < 6:
print('Take a guess.\n')
guess = input()
guess = int(guess)
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, You guessed my number in ' + guessesTaken + ' guesses!')
if guess != number:
number = str(number)
print('Nope. The number I was thinking of was ' + number)
You never reassign guess within the loop, so the truth value of guess != numb never changes. Put guess = before the input() call within the loop, or restructure it to a while True: ... break layout. Also, you only give the user another chance to guess the number if they get it exactly correct. Read through your code slowly and try to follow along with what the interpreter is doing.

Categories

Resources