PLS help with my simple asssignment, a guessing game in python

.

from random import *
num=randint(0,9)
print (“I chose a number from 0 to 9 and I want you to guess it.”)
I chose a number from 0 to 9 and I want you to guess it.
print (“You have three tries to guess the number.”)
You have three tries to guess the number.
guess1 = input("1st guess: ")
1st guess:

It doesn’t do anything, Am i missing something?

from the instructions you only implemented:

A program that generates a random number from 0 to 9 and asks the user to guess it.

and:

The program asks the user’s first guess.

the rest is still missing

from random import randrange

random_number = randrange(0,9)
count = 0
#Let the games begin!
while count < 3:
#ask user to take a guess
guess = int(raw_input("Enter a guess: "))
#print the anwer to test the winning condition
if guess ==random_number:
print ‘Congratulations! You win!’
#end the game
break
else:
#print this message only on the first 2 attempts
if count < 2:
print ‘Try again. You have ‘+str(2-count)+’ guesses left’
#proceed to the next guess attempt
count +=

SyntaxError: invalid syntax

while count < 3:
#ask user to take a guess
guess = int(raw_input("Enter a guess: "))
#print the anwer to test the winning condition
if guess ==random_number:
print ‘Congratulations! You win!’
#end the game
break
else:
#print this message only on the first 2 attempts
if count < 2:
print ‘Try again. You have ‘+str(2-count)+’ guesses left’
#proceed to the next guess attempt
count += 1
else:

SyntaxError: invalid syntax

>>> while count < 3:
	#ask user to take a guess
	guess = int(raw_input("Enter a guess: "))
	#print the anwer to test the winning condition
	if guess ==random_number:
		print 'Congratulations! You win!'
		#end the game
		break
	else:
		#print this message only on the first 2 attempts
		if count < 2:
			print 'Try again. You have '+str(2-count)+' guesses left'
		#proceed to the next guess attempt
		count += 1
else:
	print 'Sorry, you lose. The number was ' + str(random_number)

Enter a guess: 2
Try again. You have 2 guesses left
Enter a guess: 3
Congratulations! You win!

Is this correct?

seems you still miss to inform the user if there guess is higher or lower then the random number

i would simplify the else clause:

else:
    count += 1
    print "Try again. You have {:d} guesses left".format(count)

.format() offers a neat way to concat string and variable

1 Like

from random import randrange

random_number = randrange(0,9)
count = 0
#Let the games begin!
while count < 3:
#ask user to take a guess
guess = int(raw_input("Enter a guess: "))
#print the anwer to test the winning condition
if guess ==random_number:
print ‘Congratulations! You win!’
#end the game
break
else:
count += 1
print “Try again. You have {:d} guesses left”.format(count)
else:
print 'Sorry, you lose. The number was ’ + str(random_number)

Traceback (most recent call last):
File “<pyshell#19>”, line 1, in
while count < 3:
NameError: name ‘count’ is not defined

it’s not working?

Please start formatting your code, so i can read it properly, see here:

or here:

it’s okay now thanks

i tried

nter a guess: 5
Try again. You have 1 guesses left
Enter a guess: 6
Try again. You have 2 guesses left
Enter a guess: 8
Try again. You have 3 guesses left
Sorry, you ran out of chances. The number is 7

It should be you have 2 guesses left, you have 1 guess left

oops, yea then the condition should be the other way around:

count = 3
while count > 0
   count -= 1

which is still a bit easier then what you had.

But then you should realize that you should swap it around

this for me, is a more logic design. Given the user starts out with 3 guesses, and exits when he has none left

ok thanks for the quick response

I can also make mistakes, i am just a human being.

Do you understand why starting count (which you could rename to guesses_left) is a slightly more logic approach? This way, we don’t have to do math when we want to print the number of remaining guesses

what if I put something before that like:
I chose a number for 0 to 9, I want you to guess it.
You have three tries to guess the number
1st guess: 5
Sorry your guess is wrong but I’ll give you a hint
My number is greater than your guess

Something like that uh hehe i’ll try to google something like that

you could use if and else to determine if guess is greater then random_number, else is must be smaller, so then you can inform the user

its a basic if/else, if you are a bit familiar with programming and or python, this should be a piece of cake.

:joy: thanks very much

Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)] on win32
Type “copyright”, “credits” or “license()” for more information.

from random import randrange
def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
try:
guess = int(raw_input("Enter a guess: "))
if guess ==random_number:

SyntaxError: invalid syntax

def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
try:
guess = int(raw_input("Enter a guess: "))
if guess == random_number:

SyntaxError: invalid syntax

def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
guess = int(raw_input("Enter a guess: "))
if guess == random_number:
print ‘Congratulations! You win!’
break
if guess < random_number:
count -= 1
print ‘Sorry your guess is too low.You have ‘+str(2-count)+’ guesses left’
if guess > random_number:
count -= 1
print ‘Sorry your guess is too high.You have ‘+str(2-count)+’ guesses left’
else:

SyntaxError: invalid syntax

def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
guess = int(raw_input("Enter a guess: "))
if guess == random_number:
print ‘Congratulations! You win!’
break
if guess < random_number:
count -= 1
print ‘Sorry your guess is too low.You have ‘+str(2-count)+’ guesses left’
else guess > random_number:
count -= 1
print ‘Sorry your guess is too high.You have ‘+str(2-count)+’ guesses left’
else:
print 'Sorry, you ran out of chances. The number is ’ + str(random_number)

SyntaxError: invalid syntax

What’s wrong with this?

Please format your code properly:

How do I format code in my posts?

or:

How do I format code in my posts?

come on, look at what you posted. Its really difficult to read

'Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)] on win32
Type “copyright”, “credits” or “license()” for more information.

from random import randrange
def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
try:
guess = int(raw_input("Enter a guess: "))
if guess ==random_number:

SyntaxError: invalid syntax

def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
try:
guess = int(raw_input("Enter a guess: "))
if guess == random_number:

SyntaxError: invalid syntax

def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
guess = int(raw_input("Enter a guess: "))
if guess == random_number:
print ‘Congratulations! You win!’
break
if guess < random_number:
count -= 1
print ‘Sorry your guess is too low.You have ‘+str(2-count)+’ guesses left’
if guess > random_number:
count -= 1
print ‘Sorry your guess is too high.You have ‘+str(2-count)+’ guesses left’
else:

SyntaxError: invalid syntax

def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
guess = int(raw_input("Enter a guess: "))
if guess == random_number:
print ‘Congratulations! You win!’
break
if guess < random_number:
count -= 1
print ‘Sorry your guess is too low.You have ‘+str(2-count)+’ guesses left’
else guess > random_number:
count -= 1
print ‘Sorry your guess is too high.You have ‘+str(2-count)+’ guesses left’
else:
print 'Sorry, you ran out of chances. The number is ’ + str(random_number)

SyntaxError: invalid syntax

def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
guess = int(raw_input("Enter a guess: "))
if guess == random_number:
print ‘Congratulations! You win!’
break
if guess < random_number:
count -= 1
print ‘Sorry your guess is too low.You have ‘+str(2-count)+’ guesses left’
else guess > random_number:
print ‘Sorry your guess is too high.You have ‘+str(2-count)+’ guesses left’
else:
print 'Sorry, you ran out of chances. The number is ’ + str(random_number)

SyntaxError: invalid syntax

def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
guess = int(raw_input("Enter a guess: "))
if guess == random_number:
print ‘Congratulations! You win!’
break
elif guess < random_number:
count -= 1
print ‘Sorry your guess is too low.You have ‘+str(2-count)+’ guesses left’
else guess > random_number:
count -= 1
print ‘Sorry your guess is too high.You have ‘+str(2-count)+’ guesses left’
else:
print 'Sorry, you ran out of chances. The number is ’ + str(random_number)

SyntaxError: invalid syntax

‘’’
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)] on win32
Type “copyright”, “credits” or “license()” for more information.

from random import randrange
def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
try:
guess = int(raw_input("Enter a guess: "))
if guess ==random_number:

SyntaxError: invalid syntax

def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
try:
guess = int(raw_input("Enter a guess: "))
if guess == random_number:

SyntaxError: invalid syntax

def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
guess = int(raw_input("Enter a guess: "))
if guess == random_number:
print ‘Congratulations! You win!’
break
if guess < random_number:
count -= 1
print ‘Sorry your guess is too low.You have ‘+str(2-count)+’ guesses left’
if guess > random_number:
count -= 1
print ‘Sorry your guess is too high.You have ‘+str(2-count)+’ guesses left’
else:

SyntaxError: invalid syntax

def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
guess = int(raw_input("Enter a guess: "))
if guess == random_number:
print ‘Congratulations! You win!’
break
if guess < random_number:
count -= 1
print ‘Sorry your guess is too low.You have ‘+str(2-count)+’ guesses left’
else guess > random_number:
count -= 1
print ‘Sorry your guess is too high.You have ‘+str(2-count)+’ guesses left’
else:
print 'Sorry, you ran out of chances. The number is ’ + str(random_number)

SyntaxError: invalid syntax

def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
guess = int(raw_input("Enter a guess: "))
if guess == random_number:
print ‘Congratulations! You win!’
break
if guess < random_number:
count -= 1
print ‘Sorry your guess is too low.You have ‘+str(2-count)+’ guesses left’
else guess > random_number:
print ‘Sorry your guess is too high.You have ‘+str(2-count)+’ guesses left’
else:
print 'Sorry, you ran out of chances. The number is ’ + str(random_number)

SyntaxError: invalid syntax

def start_guess():
random_number=randrange(0,9)
print(“I chose a number for 0 to 9, I want you to guess it.”)
while count < 3:
guess = int(raw_input("Enter a guess: "))
if guess == random_number:
print ‘Congratulations! You win!’
break
elif guess < random_number:
count -= 1
print ‘Sorry your guess is too low.You have ‘+str(2-count)+’ guesses left’
else guess > random_number:
count -= 1
print ‘Sorry your guess is too high.You have ‘+str(2-count)+’ guesses left’
else:
print 'Sorry, you ran out of chances. The number is ’ + str(random_number)

SyntaxError: invalid syntax

‘’’

Is it really that difficult? here:

else guess > random_number:

else can’t have a condition since its everything else (all remaining scenarios), by setting a condition you might get unhandled scenarios

one version of your code should be enough?