Кажется, сейчас мои операторы if не регистрируют, что другая переменная равна указанной. Я даже напечатал сохраненную переменную, которую нужно проверить, и и предположение, и сохраненная переменная должны быть равны друг другу.
Я пытался сделать его чувствительным к регистру и без него. Я попытался отобразить переменную, чтобы затем ввести ее точно, и это просто не сработает.
import random
from random import randint
loop = 0
score = 0
f = open("songs.txt", "r")
line_number = randint(0,3)
lines = f.readlines()
songname = lines[line_number]
firstletters = songname.split()
letters = [firstletters[0] for firstletters in firstletters]
print(" ".join(letters))
f.close()
f = open("artists.txt", "r")
lines = f.readlines()
artistname = lines[line_number]
print("The first letter of each word in the title of the song is: " + "".join(letters))
print("The artist of the above song is: " + artistname)
print(songname)
answer = songname
guess = input("What is your guess? ")
if guess.lower()==answer:
score = score + 3
print("Correct! You have earned three points, and now have a total of:",score, "points!")
else:
print("Incorrect! You have one more chance to guess it correctly!")
guesstwo = input("What is your second guess? ")
if guesstwo.lower()==answer:
score = score + 1
print("Correct! You have earned one point, and now have a total of:",score, "points!")
else:
print("Incorrect! Unfortunately you have guessed incorrectly twice- therefore the game has now ended. You had a total of:",score,"points!")
Если переменная "угадай" равна переменной songname
, то она должна отображать сообщение "Correct! You have earned three points, and now have a total of:",score, "points!"
, хотя прямо сейчас она всегда отображает сообщение Song is Incorrect
.
Песни, хранящиеся в файле:
Africa
Redding
Follow
Fleekes
islice
, чтобы получить нужную строку непосредственно из итерации (не читая больше, чем вам действительно нужно). Например,songname = next(islice(f, linenumber, None)).strip()
. Вам по-прежнему нужно читать каждую строку, пока вы не получите ту, которую хотите, но каждая ненужная строка отбрасывается по мере чтения, а не сохраняется в памяти. - person chepner   schedule 09.01.2019