嘿,我正在做一个游戏,你必须猜一首只有第一个字母的歌曲的名字,如果你猜错了,一旦你两次得到更多的字母,游戏就结束了。(未完成)但我遇到了我对歌曲 csv 文件的阅读没有结束并且永远循环的地方。我不得不使用 ^C 键盘中断来结束它。该代码有时有效,有时则无效。
import random
path = __file__.replace("main.py", "")
file = open(path + "users.csv", "r+")
writer = csv.writer(file)
header = next(file)
logged = False
answer = input("Sign up or Log in? (sign/log)\n")
if answer == "sign":
username = input("Username: ")
password = input("Password: ")
answer = input("Confirm Password: ")
while answer != password:
print("Passwords do not match")
password = input("Password: ")
answer = input("Confirm Password: ")
writer.writerow([username, password, 0])
logged = True
elif answer == "log":
username = input("Username: ")
password = input("Password: ")
for line in file:
if username in line:
if password in line:
print("Hey there " + username + ", you have been logged in!")
logged = True
break
else:
print("Your password is incorrect. Please start again.")
exit()
if logged != True:
try:
print("User " + username + " is not found. Please try again or make an account.")
exit()
except:
print("Something went wrong please try again.")
exit()
file.close()
songFile = open(path + "songs.csv", "r")
csvreader = csv.reader(songFile)
header = next(csvreader)
score = 0
while True:
randomSongID = str(random.randint(1, 603))
for row in csvreader:
if randomSongID == row[0]:
randomSong = row[1]
actualSong = randomSong
artist = row[2]
randomSong = randomSong.split(" ")
for i in range(len(randomSong)):
randomSong[i] = randomSong[i][0] + (len(randomSong[i]) - 1) * "_"
print(actualSong)
randomSong = " ".join(randomSong)
answer = input(randomSong + " By " + artist+ "\n" + randomSong[0])
if answer != actualSong[1:]:
print("You have got it incorrect once. You have one more chance here is a hint.")
hint = list(actualSong[::2])
hint = "_".join(hint)
print(hint)
answer = input(randomSong[0])
if answer != actualSong[1:]:
print("The song name was:", actualSong)
print("You lost " + username + ". Your score was", score)
exit()
else:
score += 5
break
else:
score += 10
break
这就是我的终端的样子:
~/Project/ $ python main.py
Sign up or Log in? (sign/log)
log
Username: d
Password: d
Hey there d, you have been logged in!
Just the Way You Are
J___ t__ W__ Y__ A__ By Bruno Mars
Jno
You have got it incorrect once. You have one more chance here is a hint.
J_s_ _h_ _a_ _o_ _r
Just the Way You Are
She Looks So Perfect
S__ L____ S_ P______ By 5 Seconds of Summer
Sno
You have got it incorrect once. You have one more chance here is a hint.
S_e_L_o_s_S_ _e_f_c
Sno
The song name was: She Looks So Perfect
You lost d. Your score was 5
~/Project/ $ python main.py
Sign up or Log in? (sign/log)
log
Username: d
Password: d
Hey there d, you have been logged in!
This Is How We Do
T___ I_ H__ W_ D_ By Katy Perry
This Is How We Do
^CTraceback (most recent call last):
File "/home/ubuntu/School/Project/main.py", line 55, in <module>
for row in csvreader:
File "/usr/local/lib/python3.9/codecs.py", line 319, in decode
def decode(self, input, final=False):
KeyboardInterrupt
~/Project/ $
中断是它刚刚停止的地方没有退出或任何东西,只是静止了。任何帮助表示赞赏!
编辑:以防万一那些需要 csv 文件的人在这里是一个示例和标题。
id,title,artist,top genre,year,bpm,nrgy,dnce,dB,live,val,dur,acous,spch,pop
1,Hey, Soul Sister,Train,neo mellow,2010,97,89,67,-4,8,80,217,19,4,83
2,Love The Way You Lie,Eminem,detroit hip hop,2010,87,93,75,-5,52,64,263,24,23,82
3,TiK ToK,Kesha,dance pop,2010,120,84,76,-3,29,71,200,10,14,80
4,Bad Romance,Lady Gaga,dance pop,2010,119,92,70,-4,8,71,295,0,4,79
5,Just the Way You Are,Bruno Mars,pop,2010,109,84,64,-5,9,43,221,2,4,78
6,Baby,Justin Bieber,canadian pop,2010,65,86,73,-5,11,54,214,4,14,77
7,Dynamite,Taio Cruz,dance pop,2010,120,78,75,-4,4,82,203,0,9,77
8,Secrets,OneRepublic,dance pop,2010,148,76,52,-6,12,38,225,7,4,77
9,Empire State of Mind (Part II) Broken Down,Alicia Keys,hip pop,2010,93,37,48,-8,12,14,216,74,3,76
10,Only Girl (In The World),Rihanna,barbadian pop,2010,126,72,79,-4,7,61,235,13,4,73