#1 Master Mind
Generate a random four digit number. The player has to keep inputting four digit numbers until they guess the randomly generated number. After each unsuccessful try it should say how many numbers they got correct, but not which position they got right. At the end of the game should congratulate the user and say how many tries it took.
Solution:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++import random
def gen4digitrandom() :
gen4num = str(random.randint(1000,10000))
print('random number is :', gen4num)
return gen4num.strip()
def guess4digitnum():
guess = input("Enter a 4 Digit Number. 0 to Quit")
return guess.strip()
def checkinput(guess,random,cnt,loop):
print(loop,'. You entered :', guess)
if guess == random:
return 99 #true
else:
g1 = list(str(guess))
r1 = list(str(random))
for i in range(len(g1)):
if g1[i] in r1:
cnt += 1
# print(g1[i])
else:
pass
return cnt
def main():
num = gen4digitrandom()
#print('Random 4 digit number is :',num)
guess = guess4digitnum()
# print('You entered :', guess)
cnt = 0
loop = 0
if guess != '0':
res = checkinput(guess,num,cnt,loop)
if res == 99:
print("BINGO! You got it right! Your Number is :",guess," and Random Number is: ", num)
else:
# Increase the loop check to allow more tries.
while ( loop <10 and guess != '0' ):
if res == 99:
print("BINGO! You got it right! Your Number is :", guess, " and Random Number is: ", num)
break
else:
print("You matched :", res, " digits. Try Again! Count is ::", loop)
guess = guess4digitnum()
loop += 1
res = checkinput(guess, num, cnt, loop)
if res != 99 and loop >=10:
print("You are out of lives!!! Try Later!!")
else:
print("You lost!!")
main()
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Extension 1 # Let the user pick an easy mode which shows the user which position that they guessed correctly.
Solution:
import random
def gen4digitrandom() :
gen4num = str(random.randint(1000,10000))
print('random number is :', gen4num)
return gen4num.strip()
def guess4digitnum():
guess = input("Enter a 4 Digit Number. 0 to Quit")
return guess.strip()
def checkinput_E(guess, random, cnt, loop):
print("Easy Mode\n+++++++")
print(loop, '. You entered :', guess)
if guess == random:
return 99 # true
else:
g1 = list(str(guess))
r1 = list(str(random))
match = []
for i in range(len(g1)):
if g1[i] in r1:
print('Chekcing',g1[i],'in ',r1)
cnt += 1
#for j in range(len(g1)):
for k in range(len(g1)):
if g1[i] == r1[k]:
match.append(k)
break
else:
pass
# print(g1[i])
else:
pass
print("You matched positions :", match )
return cnt
def checkinput(guess,random,cnt,loop):
print(loop,'. You entered :', guess)
if guess == random:
return 99 #true
else:
g1 = list(str(guess))
r1 = list(str(random))
for i in range(len(g1)):
if g1[i] in r1:
cnt += 1
# print(g1[i])
else:
pass
return cnt
def main():
mode = input("Do you want start the game in E(asy) or H(ard) mode? \n Easy mode shows the matching position.")
num = gen4digitrandom()
#print('Random 4 digit number is :',num)
guess = guess4digitnum()
cnt = 0
loop = 0
if guess != '0':
if mode.strip() != 'E':
res = checkinput(guess,num,cnt,loop)
else:
res = checkinput_E(guess, num, cnt, loop)
if res == 99:
print("BINGO! You got it right! Your Number is :",guess," and Random Number is: ", num)
else:
# Increase the loop check to allow more tries.
while ( loop <10 and guess != '0' ):
if res == 99:
print("BINGO! You got it right! Your Number is :", guess, " and Random Number is: ", num)
break
else:
print("You matched :", res, " digits. Try Again! Count is ::", loop)
guess = guess4digitnum()
loop += 1
if mode.strip() != 'E':
res = checkinput(guess, num, cnt, loop)
else:
res = checkinput_E(guess, num, cnt, loop)
if res != 99 and loop >=10:
print("You are out of lives!!! Try Later!!")
else:
print("You lost!!")
main()
Extension 2 # Let the user pick a hard mode that gives five digits instead of four.
Solution:
import random
def gen4digitrandom() :
gen4num = str(random.randint(1000,10000))
print('random number is :', gen4num)
return gen4num.strip()
def guess4digitnum():
guess = input("Enter a 4 Digit Number. 0 to Quit")
return guess.strip()
def checkinput_E(guess, random, cnt, loop):
print("Easy Mode\n+++++++")
print(loop, '. You entered :', guess)
if guess == random:
return 99 # true
else:
g1 = list(str(guess))
r1 = list(str(random))
match = []
for i in range(len(g1)):
if g1[i] in r1:
print('Chekcing',g1[i],'in ',r1)
cnt += 1
#for j in range(len(g1)):
for k in range(len(g1)):
if g1[i] == r1[k]:
match.append(k)
break
else:
pass
# print(g1[i])
else:
pass
print("You matched positions :", match )
return cnt
# Hard mode - 5 digits
def gen5digitrandom() :
gen5num = str(random.randint(10000,100000))
print('random number is :', gen5num)
return gen5num.strip()
def guess5digitnum():
guess = input("Enter a 5 Digit Number. 0 to Quit")
return guess.strip()
def checkinput(guess,random,cnt,loop):
print(loop,'. You entered :', guess)
if guess == random:
return 99 #true
else:
g1 = list(str(guess))
r1 = list(str(random))
for i in range(len(g1)):
if g1[i] in r1:
cnt += 1
# print(g1[i])
else:
pass
return cnt
def main():
mode = input("Do you want start the game in E(asy) or H(ard) mode? \n Easy mode shows the matching position.")
if mode.strip() == 'E':
num = gen4digitrandom()
#print('Random 4 digit number is :',num)
guess = guess4digitnum()
else:
num = gen5digitrandom()
# print('Random 5 digit number is :',num)
guess = guess5digitnum()
cnt = 0
loop = 0
if guess != '0':
if mode.strip() != 'E':
res = checkinput(guess,num,cnt,loop)
else:
res = checkinput_E(guess, num, cnt, loop)
if res == 99:
print("BINGO! You got it right! Your Number is :",guess," and Random Number is: ", num)
else:
# Increase the loop check to allow more tries.
while ( loop <10 and guess != '0' ):
if res == 99:
print("BINGO! You got it right! Your Number is :", guess, " and Random Number is: ", num)
break
else:
print("You matched :", res, " digits. Try Again! Count is ::", loop)
guess = guess4digitnum()
loop += 1
if mode.strip() != 'E':
res = checkinput(guess, num, cnt, loop)
else:
res = checkinput_E(guess, num, cnt, loop)
if res != 99 and loop >=10:
print("You are out of lives!!! Try Later!!")
else:
print("You lost!!")
main()
No comments:
Post a Comment