[英]how do you add points on your python code
i need help with a code that can calculate the points for every guess the user try. 我需要有关代码的帮助,该代码可以为用户尝试的每次猜测计算出分数。 if the user get the answer right from the first try it will be 3 points, if its from the second guess then its 1 point. 如果用户从第一次尝试中得到正确的答案,它将是3分,如果从第二次猜测中得到的是1分。 when the user win or lose it stores his username and score in a external file. 当用户输赢时,它将用户名和得分存储在外部文件中。 when all users had a go then it displays the top 5 winners from the external file. 当所有用户参加比赛后,它将显示外部文件中排名前5位的获胜者。
i have already done the part that asks the user for the username and password and stores it in an external device. 我已经完成了要求用户输入用户名和密码并将其存储在外部设备中的部分。 i have also wrote down a code to display the artists name and the first letter of the song and give the user 2 tries. 我还写下了一个代码,以显示歌手姓名和歌曲的首字母,并为用户提供2次尝试。
username= input("Please enter your username")
password= input("Please enter your password")
f=open("usernamepassword.txt","a")
f.write(username)
f.write(" ")
f.write(password)
f.write("\n")
f.close()
import random
for x in range(0, 1):
randNum = int(random.randint(0, 1))
song = open("Songs.txt", "r")
songname = str(song.readlines()[0])
print(songname[0])
song.close()
artist = open("Artists.txt", "r")
artistname = artist.readlines()[0]
print(artistname)
artist.close()
y = 0
songGuess = input("What is the song called?")
while(y<=2):
if songGuess == songname:
print("Answer correct!")
break
else:
y = y + 1
songguess = input("Incorrect! try again")
if y == 1:#
print("GAME OVER")
break
This could work. 这可能有效。 I also edited your code a bit (it's still far from perfect). 我还对您的代码进行了一些编辑(这仍然很不完善)。
import random
for x in range(0, 1):
username= input("Please enter your username: ")
password= input("Please enter your password: ")
randNum = int(random.randint(0, 1))
with open("Songs.txt", "r") as song_f:
songname = str(song_f.readlines()[0])
print(songname[0])
with open("Artists.txt", "r") as artist_f:
artistname = artist_f.readlines()[0]
print(artistname)
songGuess = input("What is the song called?")
y = 0
score = 0
while(y<2):
if songGuess.lower() == songname.lower():
print("Answer correct!")
if (y==0):
score = 3
elif (y==1):
score = 1
break
else:
y = y + 1
songGuess = input("Incorrect! try again")
with open("usernamepasswordscore.txt","a") as f:
f.write("{} {} {}\n".format(username, password, score))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.