Jump to content

Python Help! Basic Loops Not Working

Go to solution Solved by Nineshadow,

@xGear

This should fix it :

Num1To10Guess = int(input ("What number do you guess?"))

Yup, it works, tested it myself.

 

.

Hi, I am trying to create a number guessing game, but it won't work. There is an error somewhere in the loop, its seriously doing my head in and any help would be appreciated. It won't register when the right number is entered and doesn't accept or find the number. If you paste the code from below into python shell 3.4 you will see: 

 

import random
from time import sleep
 
print ("This game is a simple guessing game") ; sleep(2)
print ("A random number will be generated") ; sleep(2)
print ("And your aim is to guess the number correctly") ; sleep(2)
 
print ("The game will start in:") ; sleep(2)
print ("3") ; sleep(1)
print ("2") ; sleep(1)
print ("1") ; sleep(1)
print ("**Game Started**")
 
Num1To10 = random.randint(1, 10)
Lives = 12
 
print ("Round 1:")
print ("A number between 1 and 10 has been generated, can you guess it? You have 3 chances to guess correctly") ; sleep(1)
 
while Lives > 0:
    Num1To10Guess = input ("What number do you guess?")
    
    if Num1To10Guess == Num1To10:
        print ("You guessed correctly, moving onto next round")
        break
    if Num1To10Guess != Num1To10:
        Lives = Lives - 1
        print ("Incorrect! Try again!")
    else:
        print ("You failed round 1 and ran out of lives")
        print ("**Game Over**")
        break
 
As you can see it is very basic but isn't working properly. I hope someone can fix it or help me, thank you very much, Ben.
Link to comment
https://linustechtips.com/topic/451048-python-help-basic-loops-not-working/
Share on other sites

Link to post
Share on other sites

@xGear

This should fix it :

Num1To10Guess = int(input ("What number do you guess?"))

Yup, it works, tested it myself.

 

.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

@xGear

This should fix it :

Num1To10Guess = int(input ("What number do you guess?"))
Yup, it works, tested it myself.

 

.

You Sir! Are a legend! Thank you so much :)

Also, do you have any suggestions on getting the game over bit to work? Thanks again :)

Link to post
Share on other sites

@xGear

This should fix it :

Num1To10Guess = int(input ("What number do you guess?"))
Yup, it works, tested it myself.

 

.

Ahh I figured it out, just shift the game over into another while loop which checks if the lives equal 0 and that works fine.

Thanks again for your help, you saved me! Awesome!!!! :)

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×