I'm new to programming with python/in general. The goal is to produce a random number and have the user guess the number, telling the user if their guess is correct, too high, or too low. For some reason, it says "too low" no matter what I do. Here's what I have so far:
import random
numberGenerated = random.randint (1, 5)
userInput = raw_input("Enter a number between one and five: ")
numberEntered = int(userInput)
while numberEntered > numberGenerated:
print "Your guess was too high"
userInput = raw_input("Enter a number between one and five: ")
while numberEntered < numberGenerated:
print "Your guess was too low"
userInput = raw_input("Enter a number between one and five: ")
else:
print "You're Correct!"
an other way to do this is by keeping a boolean value. This makes your code better structured and readable.
import random
numberGenerated = random.randint (1, 5)
numberEntered = int(raw_input("Enter a number between one and five: "))
match=False
while not match:
if numberEntered > numberGenerated:
print "Your guess was too high"
numberEntered =int(raw_input("Enter a number between one and five: "))
elif numberEntered < numberGenerated:
print "Your guess was too low"
numberEntered =int(raw_input("Enter a number between one and five: "))
elif numberEntered == numberGenerated:
print "You're Correct!"
match = True
if you wrap everything in a function you could use the boolean value to start a new game recursively.
import random
def playGame():
match=False
numberGenerated = random.randint (1, 5)
numberEntered =int(raw_input("Enter a number between one and five: "))
while not match:
if numberEntered>numberGenerated:
print "Your guess was too high"
numberEntered =int(raw_input("Enter a number between one and five: "))
elif numberEntered<numberGenerated:
print "Your guess was too low"
numberEntered =int(raw_input("Enter a number between one and five: "))
elif numberEntered==numberGenerated:
print "You're Correct!"
match = True
if match:
again =raw_input("type 'Y' to play again")
if again.upper() == "Y":
playGame()
playGame()
You should use proper logic...
try this and ask if you have any doubts
import random
numberGenerated = random.randint (1, 5)
userInput = raw_input("Enter a number between one and five: ")
numberEntered = int(userInput)
while numberEntered!=numberGenerated:
while numberEntered>numberGenerated:
print "Your guess was too high"
#print numberGenerated
userInput = raw_input("Enter a number between one and five: ")
numberEntered = int(userInput)
while numberEntered<numberGenerated:
print "Your guess was too low"
#print numberGenerated
userInput = raw_input("Enter a number between one and five: ")
numberEntered = int(userInput)
print "You're Correct!"
Related
I have made a number guessing game yet when I run it the loop only loops the
guess = raw_input("Guess the number between 1 and 50: ")
so I was wondering if anyone knew a fix. The full code is listed below:
import random
score = 1000
print "Welcome to this number guessing game!"
number = random.randint(1, 50)
while True:
guess = raw_input("Guess the number between 1 and 50: ")
if guess == number:
print "You got it right!"
print "Your score was:" + score
quit()
if guess < number:
print "Too Big"
score = score - 10
if guess > number:
print "Too Small"
score = score - 10
You want to indent your code properly to let the interpreter know what is inside the loop:
import random
score = 1000
print "Welcome to this number guessing game!"
number = random.randint(1, 50)
while True:
guess = raw_input("Guess the number between 1 and 50: ")
if guess == number:
print "You got it right!"
print "Your score was:" + score
quit()
if guess < number:
print "Too Big"
score = score - 10
if guess > number:
print "Too Small"
score = score - 10
AndreyS got it right, but you're comparing different types and thus not getting the expected output.
Here's a simple example of your game working:
import random
score = 1000
print "Welcome to this number guessing game!"
number = random.randint(1, 50)
while True:
guess = raw_input("Guess the number between 1 and 50: ")
if int(guess) == number:
print "You got it right!"
print "Your score was:" + str(score)
quit()
if int(guess) > number:
print "Too Big"
score = score - 10
if int(guess) < number:
print "Too Small"
score = score - 10
Notice that I'm casting the guess variable to an integer in order to get the comparisons right.
Your code had indentation issue
data/integer read from the command line had to be converted into 'int' form
Use 'break' to exit from while loop when condition is met instead of 'quit()'
Here is the working code :
import random
score = 1000
print "Welcome to this number guessing game!"
number = random.randint(1, 50)
print "number is", number
while True:
guess = int(raw_input("Guess the number between 1 and 50: "))
print "number is", guess
if (guess == number):
print "You got it right!"
print "Your score was:", score
break
if (guess < number):
print "Too Small"
score = score - 10
if (guess > number):
print "Too Big"
score = score - 10
I am having trouble writing the code of the basic number guessing game in Python. The objective of the game is to correctly guess a number from 1 to 10 picked at random by the program. I also put some help text that tells the users if they guessed too high or too low. I keep getting a syntax error. This is the code i wrote so far:
print "Welcome to the number guessing game"
print "I have my number..."
import random
while True:
random.randint(1, 10)
number = raw_input("What is your guess[1-10]: ")
if number > random.radint(1, 10):
print "sorry you guessed to high"
elif number < random.radint(1, 10):
print "You guessed to low"
elif number == random.radint(1, 10):
print "You guessed right thanks for playing"
break
else: raw_input("What is your guess[1-10]: ")
It is randint not radint. You are creating a new random number for every test; you should assign a variable to a random number outside of the while loop.
r = random.randint(1, 10)
You must indent if: else: blocks - Python is layout sensitive, e.g.
if number > r:
print "sorry you guessed to high"
You are comparing against a str, need to convert the input to an int().
number = int(raw_input("What is your guess[1-10]: "))
You have an unnecessary else: condition at the end.
So putting it all together:
import random
print "Welcome to the number guessing game"
print "I have my number..."
r = random.randint(1, 10)
while True:
number = int(raw_input("What is your guess[1-10]: "))
if number > r:
print "sorry you guessed to high"
elif number < r:
print "You guessed to low"
else:
print "You guessed right thanks for playing"
break
You could change the while loop to cover the condition:
import random
print "Welcome to the number guessing game"
print "I have my number..."
number = 0
r = random.randint(1, 10)
while number != r:
number = int(raw_input("What is your guess[1-10]: "))
if number > r:
print "sorry you guessed to high"
elif number < r:
print "You guessed to low"
print "You guessed right thanks for playing"
import random
def start():
print "\t\t***-- Please enter Y for Yes and N for No --***"
answer = raw_input("\t\t Would you like to play a Guessing Game?: ")
if answer == "Y"
or answer == "y":
game()
elif answer == "N"
or answer == "n":
end()
def end():
print("\t\t\t **Goodbye** ")
raw_input("\t\t\t**Press ENTER to Exit**")
def game():
print "\t\t\t Welcome to Williams Guessing Game"
user_name = raw_input("\n\t\t Please enter your name: ")
print "\n", user_name, "I am thinking of a number between 1 and 20"
print "You have 5 attempts at getting it right"
attempt = 0
number = random.randint(1, 20)
while attempt < 5:
guess = input("\t\nPlease enter a number: ")
attempt = attempt + 1
answer = attempt
if guess < number:
print "\nSorry", user_name, "your guess was too low"
print "You have ", 5 - attempt, " attempts left\n"
elif guess > number:
print "\nSorry ", user_name, " your guess was too high"
print "You have ", 5 - attempt, " attempts left\n"
elif guess == number:
print "\n\t\t Yay, you selected my lucky number. Congratulations"
print "\t\t\tYou guessed it in", attempt, "number of attempts!\n"
answer = raw_input("\n\t\t\t\tTry again? Y/N?: ")
if answer == "Y"
or answer == "y":
game()
elif answer == "N"
or answer == "n":
end()
start()
If you want the computer to guess your number, you could use a function like this:
import random
my_number = int(raw_input("Please enter a number between 1 and 20: "))
guesses = []
def make_guess():
guess = random.randint(1, 20)
while guess in guesses:
guess = random.randint(1, 20)
guesses.append(guess)
return guess
while True:
guess = make_guess()
print(guess)
if guess == my_number:
print("The computer wins!")
break
else:
print(guesses)
It's just a quick-and-dirty example, but I hope it gives you the idea. This way, the computer gets unlimited guesses, but you could easily change the while loop to limit its number of guesses.
This is a game I am currently trying to make. I am coding this game in python 3.4. it doesn't run.
# this is a guess the number game!
import random
guesses = 0
name = input("what is your name?")
number = random.randint(1, 20)
print = name + ", I am thinking of a number between 1 and 20..."
while guesses << 7:
guess = int(raw_input("Take a guess."))
guesses = guesses + 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:
guesses = str(guesses)
print ("Good job," + name + "you guessed my number in" +guesses +"guesses!")
if guess != number:
number = str(number)
print ("Nah dude, better luck next time!")
I think you meant to use < instead of <<. << and >> are bit shift operators to the left and right respectively.
Your last two if conditions are also outside your loop, and don't make much sense. You're already checking if guess == number once and breaking if that condition is met. if guess != number your already checking this by using < and > respectively.
print = ...? print syntax is print(some_stuff, ...). Indentation is also off at the top, but assuming that's just due to posting your first question.
Also, raw_input is for python2 it's just input in python3. You could clean the print statements up some with % formatters or using .format.
Fixed code: (Python 3 version since that's whats tagged in the question...)
import random
name = input("what is your name?")
number = random.randint(1, 20)
#print("%s I am thinking of a number between 1 and 20..." % name)
print(name + " I am thinking of a number between 1 and 20...")
guesses = 0
while guesses < 7:
guess = int(input("Take a guess."))
guesses += 1
if guess < number:
print ("your guess is too low!")
elif guess > number:
print ("your guess is too high!")
else:
#print("Good job %s you guessed my number in %d guesses" % (name, guesses))
print ("Good job, " + name + " you guessed my number in " + str(guesses) + " guesses!")
break
There are many errors in your program. Always include errors you get in your question. Given the syntax error you are making first get your hands dirty on python interpreter by executing simple commands. Below should help. Below is in Python 2, for Python 3 replace, raw_input() with input and print 'something' with print ('something')
1st Solution:
import random
name = raw_input("Hello! What is your name?\n")
print "Well, " + name + ", I am thinking of a number between 1 and 20"
no = random.randint(1,20)
guess = int(raw_input("Take a guess\n"))
count =1
while guess != no:
if guess < no:
print "Your guess is too low."
if guess > no:
print "Your guess is too high"
count +=1
guess = int(raw_input("Take a guess\n"))
print "Good job, %s! You guessed my number in %d guesses!" % (name ,count)
2nd Solution:
import random
def check():
global count # good example of use of global
guess = int(raw_input("Take a guess\n"))
if guess == no:
print "Good job, %s! You guessed my number in %d guesses!" %(name,count)
elif guess < no:
print "Your guess is too low."
count +=1
check()
else:
print "Your guess is too high"
count +=1
check()
name = raw_input("Hello! What is your name?\n")
print "Well, " + name + ", I am thinking of a number between 1 and 20"
no = random.randint(1,20)
global count
count =1
check()
Your code goes good, little changes can make it run!
import random
guesses = 0
name = raw_input("what is your name?") # use input() is using Python 3
number = random.randint(1, 20)
print name + ", I am thinking of a number between 1 and 20..."
while guesses < 7:
guess = int(raw_input("Take a guess."))
guesses = guesses + 1
if guess < number:
print ("your guess is too low!")
if guess > number:
print ("your guess is too high!")
if guess == number:
break
if guesses == number:
print ("Good job,", name, "you guessed my number in", guesses, "guesses!")
if guesses != number:
number = str(number)
print ("Nah dude, better luck next time!", "The number is", number)
Your program chooses the number to be guessed by selecting an integer at random in the range 1 to 1000 (inclusive). If the player's guess is incorrect, your program should loop until the player finally gets the number right. Your program should keep telling the player Too high or Too low to help the player “zero in” on the correct answer. After a game ends, the program should prompt the user to enter "y" to play again or "n" to exit the game.
My problem is how do you generate a new random integer each time they want to play again?
Here's my code.
import random
randomNumber = random.randrange(1, 1000)
def main():
print ""
number = input("I have a number between 1 and 1000. Can you guess my number? Please type your first guess: ")
guess(number)
def guess(number1):
correct = False
while not correct:
if number1 > randomNumber:
print "Too high. Try again."
print ""
elif number1 < randomNumber:
print "Too low. Try again."
print ""
elif number1 == randomNumber:
break
number1 = input ("What number do you guess? ")
if number1 == randomNumber:
playAagain = raw_input ("Excellent! You guessed the number! Would you like to play again (y or n)? ")
if playAagain == "y":
main()
main()
Take this line:
randomNumber = random.randrange(1, 1000)
and place it inside guess:
import random
def main():
print ""
number = input("I have a number between 1 and 1000. Can you guess my number? Please type your first guess: ")
guess(number)
def guess(number1):
#########################################
randomNumber = random.randrange(1, 1000)
#########################################
correct = False
while not correct:
if number1 > randomNumber:
print "Too high. Try again."
print ""
elif number1 < randomNumber:
print "Too low. Try again."
print ""
elif number1 == randomNumber:
break
number1 = input ("What number do you guess? ")
if number1 == randomNumber:
playAagain = raw_input ("Excellent! You guessed the number! Would you like to play again (y or n)? ")
if playAagain == "y":
main()
main()
Now, a new random integer will be created each time the function is called.
Put a loop in main() and initialize randomNumber at the beginning of the loop.
please try this remove four spaces starting from random to starting of while loop
import random
print " welcome to game of guessing "
print "you have 6 attempts"
num1 = random.randint(1,100)
print num1 # this is your computer generated number ( skip while running :only for test purpose)
num2 = num1+random.randint(1,15)
print " hint: number is close to " + str(num2)
count=1
while count<=6:
print " enter the number you think"
inputnum=raw_input()
if inputnum==str(num1):
print "congrats you guess the correct number"
break
elif inputnum<str(num1):
print " number is lower than correct one "
count=count+1
elif inputnum>str(num1):
print "number is greater than correct one"
count=count+1
else:
break
You just have to define your main() function like this.
def main():
global randomNumber
print ""
randomNumber = random.randrange(1, 1000)
number = input("I have a number between 1 and 1000. Can you guess my number? Please type your first guess: ")
guess(number)
And everything will be perfect. Hope this helps.