作为今年晚些时候一些课程作业的准备工作的一部分,我已经创建了一个策划人游戏,我希望能够打印出高分和它所属的人的名字,只要有人赢了游戏。我已经成功地保存了输入的用户名和他们的分数对,但是我打印高分的尝试没有成功。这是我迄今为止所做的所有代码,如果我写的任何东西都是完全错误或低效的,我深表歉意,我对python还相当陌生。在from random import randrange
import time
import operator
guesses=0
score=0
totaltime=0
correctlist=[]
nonexactscore=0
#Initiates variables
print("Enter a username")
while 1==1:
username=input()
namelist=list(username)
if " " not in namelist:
username=username.upper()
break
else:
print("Cannot enter spaces in your username")
print("Enter difficulty- 'EASY' , 'NORMAL' or 'HARD")
while 1==1:
difficulty=input()
if difficulty == "EASY" or difficulty == "NORMAL" or difficulty == "HARD":
break
else:
print("Invalid input")
#Waits for user input to be valid (EASY , NORMAL or HARD)
if difficulty=="HARD":
Numberlength=5
#Sets the length of the number to be guessed to 5 if the difficulty = hard
else:
Numberlength=4
#Otherwise sets the length of the number to be guessed to the normal length of 4
for n in range(0,Numberlength):
correctlist.append(str(randrange(0,9)))
#Creates a list of length Numberlength made from a sequence of random numbers (In string format)
while score != Numberlength:
#Continuously asks for the guess while it is incorrect (For it to be correct, the score must = the numberlength)
temptime=0
nonexactscore=0
score=0
guess=0
print("enter guess")
print(correctlist) #For debugging
temptime=int(time.time())
#initiates the timer
while 1==1:
guess=str(input())
if len(guess)==Numberlength and guess.isdigit():
break
#Validates input
else:
print("Invalid input")
for n in range(0,Numberlength):
if guess[n]==correctlist[n]:
score+=1
#Increments score variable if pairs of numbers in the guess list and the correct list match
if difficulty == "EASY":
print("You got a match in place " + str(n+1))
#Prints the position of the correct guess if the difficulty = easy
if guess[n] in correctlist and guess[n] != correctlist[n]:
nonexactscore += 1
#Increments non-exact score variable if the guess appears in the correctlist but isn't an exact match
print("Number of exact matches: " + str(score) + " Number of non-exact matches: " + str(nonexactscore) )
#Prints your score and non-exact score
if score != Numberlength:
print("Incorrect, try again!")
guesses+=1
#Increments guesses variable every time you make an incorrect guess
totaltime+=int(time.time())-temptime
#Adds the time taken for an individual guess to the totaltime
else:
print("Guesses: " + str(guesses))
print("You win!")
print("You took " + str(totaltime) + " Seconds! ")
totalscore=int(1000*(1/(float(totaltime*guesses))))
print("Score for comparison:" + str(totalscore))
with open("mastermindscores.txt","a") as f:
f.write(username + " " + str(totalscore)+ "\n")
with open("mastermindscores.txt","r") as k:
scoredict={}
for line in k:
key,val = line.split(" ")
scoredict[key]=val
print(scoredict) #for debugging
print("High score: " + max(scoredict) + " " + str(scoredict.get(max(scoredict))))
#Prints information about your victory
此代码导致txt文件如下所示:
^{pr2}$
我正在找程序打印“High score:Another name 100000”,但目前我还不太清楚它是如何决定打印哪个名字的(因为在几场比赛之后,它不会打印出高分)
任何对编码的建议或任何我可以对程序的改进,将不胜感激,谢谢。在
另外,如果你需要更多的澄清,我会看这篇文章几个小时左右。在