python - 你如何在你的python代码上添加点
问题描述
我需要一个代码帮助,该代码可以计算用户尝试的每次猜测的分数。如果用户从第一次尝试中得到正确的答案,则为 3 分,如果从第二次猜测中获得答案,则为 1 分。当用户赢或输时,它将他的用户名和分数存储在外部文件中。当所有用户都参与时,它会显示外部文件中的前 5 名获胜者。
我已经完成了询问用户用户名和密码并将其存储在外部设备中的部分。我还写了一个代码来显示艺术家的名字和歌曲的第一个字母,并给用户 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
解决方案
这可以工作。我还稍微编辑了您的代码(它仍然远非完美)。
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))
推荐阅读
- javascript - 如何在php中检索ajax发送的数据?
- php - 属于两列 laravel
- angular - 在Angular6中将用户名和密码传递给REST API
- java - 如何在 android studio 的 switch case 中添加 youtube 链接作为意图
- tkinter - matplotlib 和 tkinter 的问题:Tkinter 回调中的异常
- excel - Excel - 如果满足两个条件,则读取一行并返回 true 的公式
- docker - 如何在 Jenkins 管道内从 github 中的 Dockerfile 创建 docker 容器?
- wordpress - 如何创建 Wordpress 子插件,就像子主题一样?
- java - 如何在 Java 中创建受密码保护的 zip 文件夹?
- python - 是否可以从 ubuntu guest 使用安装在 Windows 主机中的 google cloud sdk?