Mastermind Game in Python -


i have looked @ thread python mastermind game troubles on website question asking different have been given different task.

this question:

generate random 4 digit number. player has keep inputting 4 digit numbers until guess randomly generated number. after each unsuccessful try should how many numbers got correct, not position got right. @ end of game should congratulate user , how many tries took.

i have made far:

from random import randint  n1 = randint (1,9) n2 = randint (1,9) n3 = randint (1,9) n4 = randint (1,9) numberswrong = 0 print (n1,n2,n3,n4) guess1 = input("guess first number") guess2 = input("guess second number") guess3 = input("guess third number") guess4 = input("guess fourth number") guess1 = int (guess1) guess2 = int (guess2) guess3 = int (guess3) guess4 = int (guess4) if guess1 != n1:     numberswrong +=1 else:     numberswrong +=0    if guess2 != n2:     numberswrong +=1 else:     numberswrong +=0  if guess3 != n3:     numberswrong +=1 else:     numberswrong +=0  if guess4 != n4:     numberswrong +=1 else:     numberswrong +=0  print ("you got",numberswrong, "numbers wrong") if numberswrong == 0:     print ("well done") while numberswrong != 0:     guess1 = input("guess first number")     guess2 = input("guess second number")     guess3 = input("guess third number")     guess4 = input("guess fourth number")     guess1 = int (guess1)     guess2 = int (guess2)     guess3 = int (guess3)     guess4 = int (guess4)     if guess1 != n1:         numberswrong +=1     else:         numberswrong +=0        if guess2 != n2:         numberswrong +=1     else:         numberswrong +=0      if guess3 != n3:         numberswrong +=1     else:         numberswrong +=0      if guess4 != n4:         numberswrong +=1     else:         numberswrong +=0      print ("you got",numberswrong, "numbers wrong") print ("well done") 

the print (n1,n2,n3,n4) test code , won't there in real code. having 2 problems. firstly, if guess number first time, 2 "well done"'s shown , second how clear numberswrong variable without ruining while loop.if don't numberswrong variable goes everytime incorrect guess past 4 when guess right, don't done message numberswrong equals 25 or similar. also, if numberswrong=0 @ end of while loop while loop thinks got correct , says done. don't know solution is. loop counter maybe?

help appreciated

edit: see why "well done" printed twice. second query. still can't see it.

edit no.2: @moose. answer best can. when press run program says number asks me numbers 1-4 , tells me number of numbers have got wrong. problem number of numbers goes , isn't cleared. lets got numbers wrong 3 times. on third time says got 12 numbers wrong. can't 12 numbers wrong because there 4 numbers guess. see happening while loop not clearing after each pass. think know solution. somewhere numberswrong=0 clear loop. don't know put statement.

here updated code! i’ve used variable c keep track of number of tries. break out of infinite loop when user has correctly guessed number.

from random import randint  n1 = randint(1,9) n2 = randint(1,9) n3 = randint(1,9) n4 = randint(1,9) c = 1  while true:     print (n1,n2,n3,n4)     guess1 = input("guess first number")     guess2 = input("guess second number")     guess3 = input("guess third number")     guess4 = input("guess fourth number")     guess1 = int(guess1)     guess2 = int(guess2)     guess3 = int(guess3)     guess4 = int(guess4)     numberswrong = 0      if guess1 != n1:         numberswrong += 1     if guess2 != n2:         numberswrong += 1      if guess3 != n3:         numberswrong += 1      if guess4 != n4:         numberswrong += 1      if numberswrong == 0:         print('well done!')         print('it took ' + str(c) + ' ries guess number!')         break     else:         print('you got ' + str(4-numberswrong) + ' numbers right.')     c += 1 

Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -