Ask coding questions

← Back to all posts
I need a little help on this Number Guessing Games's attempt counter
HarryPall (0)

This Is My Game :

print ("What Is Your Name?")
name = input()
print ("Hello " + name + ", let's play Guess The Number!")
print ("Now Guess!")
import random
Num = random.randint(1, 101)
userattempts = 0
while userattempts < 10:
userguess = int(input())
if Num > userguess:
print ("Too Low!")
userattempts + 1
continue
if Num < userguess:
print ("Too High")
userattempts + 1
continue
if Num == userguess:
print ("Perfect, You Got The Number!")
break
while userattempts == 10:
print ("Game Over, You Have Used Too Many Turns")
break

But the loop won't print ("Game Over, You Have Used Too Many Turns") and break if the user has attempted 10 attempts

Commentshotnewtop
RyanRana (168)

The code is pretty clean but you did make one common mistake. When you want to add or subtract from a variable there are two ways to do so. For variable n, if you want to add one you can do, n = n+1 or, n+=1. When we're adding 1 to user attempts you did userattempts+1. This is okay since this is a pretty common mistake for beginners. I fixed that when reviewing your code.

Also, you do not need to say continue after everything. Python will do that on its own. You would only want to do that when dealing with very large programs handling very large data, just to save time. This is a simple program, so it works very fast.

And one last thing the last while loop is doing the same thing as and if statement would do the same as this so I changed that too. But it worked fine before whatever you want.

This is pretty good code and I am assuming you are relatively new to python. I have a youtube channel that you might want to check out. https://www.youtube.com/channel/UCIta8OqsKjfE1knTU2DJlSQ/playlists?disable_polymer=1 . Please subscribe and show your support!

Here is the new and improved code - in format!

print ("What Is Your Name?")
name = input()
print ("Hello " + name + ", let's play Guess The Number!")
print ("Now Guess!")
import random
Num = random.randint(1, 101)
userattempts = 0
while userattempts < 10:
userguess = int(input())
if Num > userguess:
print ("Too Low!")
userattempts+=1
if Num < userguess:
print ("Too High")
userattempts+=1
if Num == userguess:
print ("Perfect, You Got The Number!")
break
if user attempts == 10:
print ("Game Over, You Have Used Too Many Turns")