Hi guys, I'm new to this website so if I made some mistake please forgive. :)
I just want to learn more about Python , I'm new to Python and i'm trying to improve.
Here is my work (took some help from Daniweb) i just want to ask that how can i change the guessing turn (0,17) base on the player want to.
Below this i only know how to insert 1 number of guessing ( Ex : 6)

``````import random

print ("Welcome to my game !")
l= input("Please enter the level you want to play (1:easy,2:hard): ")
l=int(l)

r= input("How many rounds do you want to play? (even number only) ")
r=int(r)

print (" You only get 6 turns to guess the letter")
print ("Player 1's turn to play the game")

guess = 6
letter_g= ()
while guess !=0:
letter = input('Guess a letter (a-z): ')
if letter in letter_g:
print('Please enter another letter')
else:
guess = guess - 1``````

Please help me and sorry for my poor english :sweat:
I'm use Python v.3
Thank you

## All 13 Replies

I do not understand what you mean by (0, 17), I do not see anything like that in your code. You are suppose to update letter_g aren't you?

Do you mean that you want to be able to set the number of turns that the user can guess? It appears that you already do that, all you would need to do is set the number of guesses to r? Or are turns and rounds in the code not the same thing?

I don't know if I should post this here, but here's a code snippet from a game I made, some time ago, while reading the book of Al Sweigart, Invent your own computer games with Python (and I recommend this book if you want to do such games - and it's free for download in PDF, look it up on Google.)
Maybe this will help you a bit, in your guessing game...
The main difference is that my game is based on finding the number which the computer chose, not a letter as in your case...

``````'''
Created on Feb 01, 2012

@author: sin
'''
import random
import sys
import time
class guess:
def __init__(self):
pass
def showUi(self):
while True:
print """
Welcome to Guess my number."""
self.name = raw_input("What is your name? \n")
print "Greetings", self.name, "and welcome to Guess my number."
print """
The game is really simple. First, you have to choose a level, than at each level,
depending, you will have to guess a number which I will randomly choose. Don't worry
if you don't get if from the first time, I will be instructing you if your guess is
higher or lower of the chosen number. Note that you have limited guesses per each
level, so try not to guess foolishly.
Well, enough talk, let's see the menu.
Levels:
1. Level 1 - Easy level, with numbers in range of 0 to 20, and with 6 guesses.
2. Level 2 - Medium level, with numbers in range of 0 to 30, and with 6 guesses.
3. Level 3 - Hard level, for players in luck, with numbers in range of 0 to 100,
and with 5 guesses.
"""

cmd = raw_input("Please, insert your command: ")
if cmd == "1" or cmd == "1.":
self.level1()
elif cmd == '' or cmd == "":
elif cmd == "2" or cmd == "2.":
self.level2()
elif cmd == "3" or cmd == "3.":
self.level3()
elif cmd == "exit" or cmd == "EXIT" or cmd == "quit" or cmd == "destroy" or cmd == "bambucha" or cmd == "detonate" or cmd == "kill" or cmd == "explode":
sys.exit()

def redo(self):
print """
Levels:
1. Level 1 - Easy level, with numbers in range of 0 to 20, and with 6 guesses.
2. Level 2 - Medium level, with numbers in range of 0 to 30, and with 6 guesses.
3. Level 3 - Hard level, for players in luck, with numbers in range of 0 to 100,
and with 5 guesses.
"""
while True:
cmd = raw_input("Please, insert your command: ")
if cmd == "1" or cmd == "1.":
self.level1()
elif cmd == "2" or cmd == "2.":
self.level2()
elif cmd == "3" or cmd == "3.":
self.level3()
elif cmd == "exit" or cmd == "EXIT" or cmd == "quit" or cmd == "destroy" or cmd == "bambucha" or cmd == "detonate" or cmd == "kill" or cmd == "explode":
sys.exit()

def level1(self):
guesstaken = 0
print "Ok", self.name + ',', "you chose level 1. Lets see if you can guess my number."
print "I will think of a number between 0 and 20, and you have 6 tries to guess that number."

number = random.randint(0, 20)

while guesstaken < 6:
guess = int(raw_input("Take a guess: \n"))
guesstaken += 1
if guess < number: print "Your guess is lower than the number I've chosen."
elif guess > number: print "Your guess is higher than the number I've chosen."
else: break

if guess == number:
print "Congratulation", self.name, "you have guessed my number,", str(number) + ',', "from ", guesstaken, "guesses."
else: print "I am sorry. The number I choose was", str(number) + '.', "Unfortunately you're out of tries, better luck next time."
d = raw_input("Try again? Y/N. \n")
if d == "Y" or d == "y" or d == "Yes" or d == "Ok" or d == "ok" or d == "Sure":
self.redo()
elif d == "n" or d == "N" or d == "No" or d == "no":
print "Thank you", self.name, "for playing Guess my number."
time.sleep(5)
sys.exit()

def level2(self):
guesstaken = 0
print "Ok", self.name + ',', "you chose level 2. Lets see if you can guess my number."
print "I will think of a number between 0 and 40, and you have 8 tries to guess that number."

number = random.randint(0, 40)

while guesstaken < 8:
guess = int(raw_input("Take a guess: \n"))
guesstaken += 1
if guess < number: print "Your guess is lower than the number I've chosen."
elif guess > number: print "Your guess is higher than the number I've chosen."
else: break

if guess == number:
print "Congratulation", self.name, "you have guessed my number", str(number) + ',', "from ", guesstaken, "guesses."
else: print "I am sorry. The number I choose was", str(number) + '.', "Unfortunately you're out of tries, better luck next time."
d = raw_input("Try again? Y/N. \n")
if d == "Y" or d == "y" or d == "Yes" or d == "Ok" or d == "ok" or d == "Sure":
self.redo()
elif d == "n" or d == "N" or d == "No" or d == "no":
print "Thank you", self.name, "for playing Guess my number."
time.sleep(5)
sys.exit()

def level3(self):
guesstaken = 0
print "Ok", self.name + ',', "you chose level 3. Lets see if you can guess my number."
print "I will think of a number between 0 and 100, and you have 10 tries to guess that number."

number = random.randint(0, 100)

while guesstaken < 10:
guess = int(raw_input("Take a guess: \n"))
guesstaken += 1
if guess < number: print "Your guess is lower than the number I've chosen."
elif guess > number: print "Your guess is higher than the number I've chosen."
else: break

if guess == number:
print "Congratulation", self.name, "you have guessed my number", str(number) + ',', "from ", guesstaken, "guesses."
else: print "I am sorry. The number I choose was", str(number) + '.', "Unfortunately you're out of tries, better luck next time."
d = raw_input("Try again? Y/N. \n")
if d == "Y" or d == "y" or d == "Yes" or d == "Ok" or d == "ok" or d == "Sure":
self.redo()
elif d == "n" or d == "N" or d == "No" or d == "no":
print "Thank you", self.name, "for playing Guess my number."
time.sleep(5)
sys.exit()

m = guess()
m.showUi()``````

Do you mean that you want to be able to set the number of turns that the user can guess? It appears that you already do that, all you would need to do is set the number of guesses to r? Or are turns and rounds in the code not the same thing?

Hi Griever,
ya i mean like if the program ask the player "How many time you want to guess?" if the player enter the input answer is 5 times then he only gets 5 times, and if 9 then 9 times but i don't know how to set that.

I don't know if I should post this here, but here's a code snippet from a game I made, some time ago, while reading the book of Al Sweigart, Invent your own computer games with Python (and I recommend this book if you want to do such games - and it's free for download in PDF, look it up on Google.)
Maybe this will help you a bit, in your guessing game...
The main difference is that my game is based on finding the number which the computer chose, not a letter as in your case...

``````'''
Created on Feb 01, 2012

@author: sin
'''
import random
import sys
import time
class guess:
def __init__(self):
pass
def showUi(self):
while True:
print """
Welcome to Guess my number."""
self.name = raw_input("What is your name? \n")
print "Greetings", self.name, "and welcome to Guess my number."
print """
The game is really simple. First, you have to choose a level, than at each level,
depending, you will have to guess a number which I will randomly choose. Don't worry
if you don't get if from the first time, I will be instructing you if your guess is
higher or lower of the chosen number. Note that you have limited guesses per each
level, so try not to guess foolishly.
Well, enough talk, let's see the menu.
Levels:
1. Level 1 - Easy level, with numbers in range of 0 to 20, and with 6 guesses.
2. Level 2 - Medium level, with numbers in range of 0 to 30, and with 6 guesses.
3. Level 3 - Hard level, for players in luck, with numbers in range of 0 to 100,
and with 5 guesses.
"""

cmd = raw_input("Please, insert your command: ")
if cmd == "1" or cmd == "1.":
self.level1()
elif cmd == '' or cmd == "":
elif cmd == "2" or cmd == "2.":
self.level2()
elif cmd == "3" or cmd == "3.":
self.level3()
elif cmd == "exit" or cmd == "EXIT" or cmd == "quit" or cmd == "destroy" or cmd == "bambucha" or cmd == "detonate" or cmd == "kill" or cmd == "explode":
sys.exit()

def redo(self):
print """
Levels:
1. Level 1 - Easy level, with numbers in range of 0 to 20, and with 6 guesses.
2. Level 2 - Medium level, with numbers in range of 0 to 30, and with 6 guesses.
3. Level 3 - Hard level, for players in luck, with numbers in range of 0 to 100,
and with 5 guesses.
"""
while True:
cmd = raw_input("Please, insert your command: ")
if cmd == "1" or cmd == "1.":
self.level1()
elif cmd == "2" or cmd == "2.":
self.level2()
elif cmd == "3" or cmd == "3.":
self.level3()
elif cmd == "exit" or cmd == "EXIT" or cmd == "quit" or cmd == "destroy" or cmd == "bambucha" or cmd == "detonate" or cmd == "kill" or cmd == "explode":
sys.exit()

def level1(self):
guesstaken = 0
print "Ok", self.name + ',', "you chose level 1. Lets see if you can guess my number."
print "I will think of a number between 0 and 20, and you have 6 tries to guess that number."

number = random.randint(0, 20)

while guesstaken < 6:
guess = int(raw_input("Take a guess: \n"))
guesstaken += 1
if guess < number: print "Your guess is lower than the number I've chosen."
elif guess > number: print "Your guess is higher than the number I've chosen."
else: break

if guess == number:
print "Congratulation", self.name, "you have guessed my number,", str(number) + ',', "from ", guesstaken, "guesses."
else: print "I am sorry. The number I choose was", str(number) + '.', "Unfortunately you're out of tries, better luck next time."
d = raw_input("Try again? Y/N. \n")
if d == "Y" or d == "y" or d == "Yes" or d == "Ok" or d == "ok" or d == "Sure":
self.redo()
elif d == "n" or d == "N" or d == "No" or d == "no":
print "Thank you", self.name, "for playing Guess my number."
time.sleep(5)
sys.exit()

def level2(self):
guesstaken = 0
print "Ok", self.name + ',', "you chose level 2. Lets see if you can guess my number."
print "I will think of a number between 0 and 40, and you have 8 tries to guess that number."

number = random.randint(0, 40)

while guesstaken < 8:
guess = int(raw_input("Take a guess: \n"))
guesstaken += 1
if guess < number: print "Your guess is lower than the number I've chosen."
elif guess > number: print "Your guess is higher than the number I've chosen."
else: break

if guess == number:
print "Congratulation", self.name, "you have guessed my number", str(number) + ',', "from ", guesstaken, "guesses."
else: print "I am sorry. The number I choose was", str(number) + '.', "Unfortunately you're out of tries, better luck next time."
d = raw_input("Try again? Y/N. \n")
if d == "Y" or d == "y" or d == "Yes" or d == "Ok" or d == "ok" or d == "Sure":
self.redo()
elif d == "n" or d == "N" or d == "No" or d == "no":
print "Thank you", self.name, "for playing Guess my number."
time.sleep(5)
sys.exit()

def level3(self):
guesstaken = 0
print "Ok", self.name + ',', "you chose level 3. Lets see if you can guess my number."
print "I will think of a number between 0 and 100, and you have 10 tries to guess that number."

number = random.randint(0, 100)

while guesstaken < 10:
guess = int(raw_input("Take a guess: \n"))
guesstaken += 1
if guess < number: print "Your guess is lower than the number I've chosen."
elif guess > number: print "Your guess is higher than the number I've chosen."
else: break

if guess == number:
print "Congratulation", self.name, "you have guessed my number", str(number) + ',', "from ", guesstaken, "guesses."
else: print "I am sorry. The number I choose was", str(number) + '.', "Unfortunately you're out of tries, better luck next time."
d = raw_input("Try again? Y/N. \n")
if d == "Y" or d == "y" or d == "Yes" or d == "Ok" or d == "ok" or d == "Sure":
self.redo()
elif d == "n" or d == "N" or d == "No" or d == "no":
print "Thank you", self.name, "for playing Guess my number."
time.sleep(5)
sys.exit()

m = guess()
m.showUi()``````

Hi lucaciandrew,
thanks for your help but its seem too much for me to understand

You should usr "r" instead of guess, and you have to add the letters guessed to letter-g.

``````r= input("How many rounds do you want to play? (even number only) ")
r=int(r)

print (" You only get %d turns to guess the letter" % (d))
print ("Player 1's turn to play the game")

#guess = 6
letter_g=[]
while r !=0:    letter = input('Guess a letter (a-z): ')
if letter in letter_g:
print('Please enter another letter')
else:
letter_g.append(letter)
r = r - 1``````

You should usr "r" instead of guess, and you have to add the letters guessed to letter-g.

``````r= input("How many rounds do you want to play? (even number only) ")
r=int(r)

print (" You only get %d turns to guess the letter" % (d))
print ("Player 1's turn to play the game")

#guess = 6
letter_g=[]
while r !=0:    letter = input('Guess a letter (a-z): ')
if letter in letter_g:
print('Please enter another letter')
else:
letter_g.append(letter)
r = r - 1``````

Hi woooee,
Thanks for your help.
May i ask what is %d means in this case ? is that mod d ?
and about the append, I dont know append use in what case , i just study about basic one so i don't know it , sorry :(

Oh I got what you show me now, thank so much for your help guys ^_^ but i still don't understand why should i put %

should read up more, have gone through the tutorial in the documentation for example? Here one example explaining the string formatting "old" style. Http://network-theory.co.uk/docs/pytut/FancierOutputFormatting.html It is good to start to learn the more flexible format method also.

should read up more, have gone through the tutorial in the documentation for example? Here one example explaining the string formatting "old" style. Http://network-theory.co.uk/docs/pytut/FancierOutputFormatting.html It is good to start to learn the more flexible format method also.

Hi Tony,
Thanks for your website, I was reading about it but still no clue cause in class my teacher said % mean mod, and only using the number to give the example like if i%2==0 and so on but about %r I really have no clue.

Why don't you try it yourself?

``````>>> print('%d + %d = %d' % (3, 4, 3+4))
3 + 4 = 7
>>> print('Hello, %s! %d + %d = %d' % ('Kitty', 3, 4, 3+4))
Hello, Kitty! 3 + 4 = 7
>>> print('Hello, %(name)s! %(a)d + %(b)d = %(c)d' % dict(name='Kitty', a=3, b=4, c=3+4))
Hello, Kitty! 3 + 4 = 7
>>>``````

Why don't you try it yourself?

``````>>> print('%d + %d = %d' % (3, 4, 3+4))
3 + 4 = 7
>>> print('Hello, %s! %d + %d = %d' % ('Kitty', 3, 4, 3+4))
Hello, Kitty! 3 + 4 = 7
>>> print('Hello, %(name)s! %(a)d + %(b)d = %(c)d' % dict(name='Kitty', a=3, b=4, c=3+4))
Hello, Kitty! 3 + 4 = 7
>>>``````

Thank you very much Tony :D thanks!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.