The code allows me more than six times for input and also it did not print the else statement. My code is:
import random
secret = random.randint(1, 99)
guess = 0
tries = 0
print ('AHOY! I am the Dread Prites Roberts , and i have a secret!')
print ('It is a number from 1 to 99. I\'ll give you 6 tries ')
while guess != secret and tries < 6:
guess = int(input('What is your guess? '))
if guess < secret:
print ('Too Low, you scurvy dog!')
elif guess > secret:
print ('Too high, boy')
tries = tries + 1
elif guess == secret:
print ('Avast! you got it ! Found my seceret , you did!')
else:
print ('No more guess! Better Luck next time')
print ('The secret number was',secret)
I tried the code in Python 3.4. It prints the result more than six times. While guess is not equal to secret and tries... after 6 tries it will print 'No more guess better luck next time' but is executing again and again
your have an indentation problem (i guess happened by pasting) but your main problem is, that you are only incrementing tries when the guess was too high. Also you should move the last if else out of the while block, since the while condition is already taking care of vars.
Your implementation should look like this:
import random
secret = random.randint(1, 99)
guess = 0
tries = 0
print ('AHOY! I am the Dread Prites Roberts , and i have a secret!')
print ('It is a number from 1 to 99. I\'ll give you 6 tries ')
while guess != secret and tries < 6:
guess = int(input('What is your guess? '))
tries = tries + 1
if guess < secret:
print ('Too Low, you scurvy dog!')
elif guess > secret:
print ('Too high, boy')
if guess == secret:
print ('Avast! you got it ! Found my seceret , you did!')
else:
print ('No more guess! Better Luck next time')
print ('The secret number was',secret)
Related
I need to keep track of the number of guesses a user inputs in a simple guessing game.
I have tried using attempts= 0 and then setting attempts to = attempts + 1. Even when I do this, the code will print "You have guessed in 1 attempts" even when the user has guessed in more attempts than one.
Code:
attempts = 0;
print("Hello, welcome to the game. You will be choosing a number
between 1 and 100. You can only guess up to 10 times.")
for tries in range(tries_allowed):
print("You only get 10 tries.")
break
while attempts < 10:
guess = int(input("Please guess a number"));
attempts_used= attempts + 1;
if guess > random_number:
print("Guess is too high, try a smaller number");
elif guess < random_number:
print("Guess is too low, try a higher number");
elif guess == random_number:
attempts_used=str(attempts_used)
print("Correct- you win in", attempts_used, "guesses");
exit();
else:
if tries_allowed == 10:
print("You failed to guess in time")
my_list= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
my_list.append(attempts_used)
print(my_list)
You never update the attempts variable, you've created a new one called attempts_used, you don't need to do this.
Just use attempts everywhere you're using attempts_used
Note: Whilst you're at it you should get rid of what is known as a "magic number", or a hard coded limit in your while loop
while attempts < tries_allowed:
Cleaned up your code a bit, shows the += counting method working for your script.
As others have said, the original code is creating an entirely new variable attempts_used that is simply attempts + 1, and attempts remains 0.
It could also be attempts = attempts + 1, += means the same thing.
To make a int a str in python for printing purposes, it does not need to be stored to a separate variable, just call str() around it, unless you plan to use the string separately.
import random
random_number = random.randint(1,100)
attempts = 0
tries_allowed = 10
print("Hello, welcome to the game. You will be choosing a number between 1 and 100")
print("You only get " + str(tries_allowed) + " tries.")
my_list = []
while attempts < tries_allowed:
guess = int(input("Please guess a number: "))
if guess in my_list:
print("You have already guessed " + str(guess))
continue
attempts += 1
my_list.append(guess)
if guess > random_number:
print("Guess is too high, try a smaller number")
elif guess < random_number:
print("Guess is too low, try a higher number")
elif guess == random_number:
print("Correct- you win in", str(attempts), "guesses")
break
else:
if attempts == 10:
print("You failed to guess in time")
for item in my_list:
print(item)
Your "attemps" variable stays on 0, so attemps_used(attempts + 1) will always be 1. You have to merge both of the variables in only one in order to control it (attempts=attempts+1)
Code which also checks previous input and prints a message.
import random
# Hello World program in Python
attempts = 0
print("Hello, welcome to the game. You will be choosing a number between 1 and 100. You can only guess up to 10 times.")
random_number = random.randint(1, 101)
#print(random_number)
my_list= []
for tries in range(10):
print("You only get 10 tries.")
guess = int(input("Please guess a number: "))
if guess in my_list:
print("You already guessed this number!!")
my_list.append(guess)
attempts += 1
if guess > random_number:
print("Guess is too high, try a smaller number")
elif guess < random_number:
print("Guess is too low, try a higher number")
else:
print("Correct- you win in", tries + 1, "guesses")
attempts = -1
break
if attempts is 10 and attempts is not -1:
print("You failed to guess in time")
print("Attempts : ", my_list)
#Write a short program that will do the following
#Set a value your favorite number between 0 and 100
#Ask the user to guess your favorite number between 0 and 100
#Repeat until they guess that number and tell them how many tries it took
#If the value they guessed is not between 0 and 100
#tell the user invalid guess and do not count that as an attempt
My problem is that even if the user guesses a number between 0 and 100, it still prints out the "Invalid guess. Try again". How do I control my loop to skip past the print statement and question repeat if it's acceptable input(1-100)? Thanks in advance!
favoriteNumber = 7
attempts = 0
guess = raw_input("Guess a number between 0 and 100: ")
if (guess < 0) or (guess > 100):
print "Invalid guess. Try again"
guess = raw_input("Guess a number between 0 and 100: ")
attempts1 = str(attempts)
print "it took " + attempts1 + "attempts."
Use input and not raw_input,so you get integer and not string
favoriteNumber = 7
attempts = 0
while True:
guess = input("Guess a number between 0 and 100: ")
if (guess < 0) or (guess > 100):
attempts=attempts+1
print "Invalid guess. Try again"
else:
attempts=attempts+1
break
attempts1 = str(attempts)
print "it took " + attempts1 + " attempts."
In Python 2.7.10 it appears that if you do not convert a string to an integer, it accepts it but all rules that apply to numbers return false.
Here is a working example:
favoriteNumber = 7
attempts = 0
guess = raw_input("Guess a number between 0 and 100: ")
if (int(guess) < 0) or (int(guess) > 100):
print "Invalid guess. Try again"
guess = raw_input("Guess a number between 0 and 100: ")
attempts1 = str(attempts)
print "it took " + attempts1 + " attempts."
In Python 3.4 the original code yields an error where it tells you that it is a string instead of an integer. But, like Paul said you could put raw_input inside of an int() command.
you raw_input returns a string, which is always > 100. Cast it to a number with int(raw_input())
I want to have a user try a guessing game. The program should loop until the user guesses right.
How can I compare the values? Right now its going through the else part every time, even when the user guesses right.
Here is the code;
import sys
from random import randint
secret_number = randint(0, 100)
num_guesses = 0
guess = 0
while guess != secret_number:
guess = raw_input("Enter a number: ")
if (guess < secret_number):
print "Your guess is too low. Please try again."
else:
print "Your guess is too high. Please try again."
num_guesses = num_guesses + 1
print "Thank you, you guessed right"
print guess
You need to convert the string that raw_input returns into an integer using int, so the comparison operator works the way you expect it to:
guess = int(raw_input("Enter a number: "))
raw_input will return string, you compare string with int and nothing works
also you will never guess the number:
your code hav 2 options: too low or too high
also you never compare tries with max tries (try to fix that by yourself)
corrected version:
import sys
from random import randint
secret_number = randint(0, 100)
num_guesses = 0
guess = 0
while guess != secret_number:
guess = raw_input("Enter a number: ")
if (int(guess) < secret_number):
print "Your guess is too low. Please try again."
elif (int(guess) > secret_number) :
print "Your guess is too high. Please try again."
else:
print "Thank you, you guessed right"
break
num_guesses = num_guesses + 1
print guess
for lp in range(100):
if guess == number:
break
if guess < number:
print "Nah m8, Higher."
else:
print "Nah m8, lower."
This is some basic code that I was told to make for a basic computing class. My aim is to make a simple 'game' where the user has to guess a random number that the computer has picked (1-100) This is a small section of the code where I want to continue checking if the guess is equal to, lower or higher than the number; but if I put a print statement below, it will print the text 100 times. How can I remove this problem?
Thanks in advance.
It seems like you're omitting the guessing stage. Where is the program asking the user for input?
Ask them at the beginning of the loop!
for lp in range(100):
guess = int(input('Guess number {0}:'.format(lp + 1)))
...
You need to get a new input each time through your loop; otherwise you just keep checking the same things.
for lp in range(100):
if guess == number:
break
if guess < number:
# Get a new guess!
guess = int(raw_input("Nah m8, Higher."))
else:
# Get a new guess!
guess = int(raw_input("Nah m8, lower."))
You should ask for a guess inside the loop:
while True:
guess = int(raw_input("Guess: "))
if guess == number:
break
if guess < number:
print "Nah m8, Higher."
else:
print "Nah m8, lower."
import random
number = 0
x = []
while number < 100:
guess = random.randint(1,100)
if number < guess:
print(f 'Number {number} is less than guess {guess}')
elif number > guess:
print(f 'Number {number} is greater than guess {guess}')
number += 1
This will work for you
and I apologies for the simple nature of my question.
Im working my way through "Hello World", Computer programming for Kids and Beginners - By Warren and Carter Sande.
The first chapters second example is this numeric example, of a pirate number quiz. Programming language is Python.
Its supposed to allow 6 guesses, unless the correct number is guessed first. I guess for you people the code would be self explanatory.
import random
secret = random.randint(1,99)
guess = 0
tries = 0
print "AHOY! I'm the dread Pirate Roberts, and I have a secret!"
print "It's a number from 1 to 99. I'll give you 6 tries."
while guess != secret and tries < 6:
guess = input("What's yer guess? ")
if guess < secret:
print "Too low, ye scurvy dog!"
elif guess > secret:
print "Too high, landlubber!"
tries = tries +1
if guess == secret:
print "Avast! Ye got it! Found my secret, ye did!"
else:
print "No more guesses! Better luck next time, matey!"
print "The secret number was", secret
But when I run it, it tells me what the answer is after each guess, which its not supposed to. Its supposed to wait until the end.
I just cant figure out what I have done wrong. I have checked each line, or at least, I think I have.
If someone could point out where I have gone wrong, that would be great.
Proper indentation is key. Make sure you indent the stuff inside the loop, and leave the stuff after the loop un-indented.
while guess != secret and tries < 6:
guess = input("What's yer guess? ")
if guess < secret:
print "Too low, ye scurvy dog!"
elif guess > secret:
print "Too high, landlubber!"
tries = tries + 1
if guess == secret:
print "Avast! Ye got it! Found my secret, ye did!"
else:
print "No more guesses! Better luck next time, matey!"
print "The secret number was", secret
A nicer way to write this might be. Then you don't have to set guess to 0 outside the loop
for tries in range(1, 6):
guess = input("What's yer guess? ")
if guess < secret:
print "Too low, ye scurvy dog!"
elif guess > secret:
print "Too high, landlubber!"
else:
print "Avast! Ye got it! Found my secret, ye did!"
break
else:
print "No more guesses! Better luck next time, matey!"
print "The secret number was", secret