Python 根据输入的总长度进行评分
预期产量Python 根据输入的总长度进行评分,python,Python,预期产量 输入:“人”,然后得分=3 输入:“你好”,然后得分=4 输入:“配给”,然后得分=8 但它只会输出0 score = 0 guess_word = input("Guess a word: ") secret_word = ["man","hello","brunch","awesome","great","estuary","rationed"] while guess_word == secret_word: if len(guess_word) == 2 and l
- 输入:“人”,然后得分=3
- 输入:“你好”,然后得分=4
- 输入:“配给”,然后得分=8
score = 0
guess_word = input("Guess a word: ")
secret_word = ["man","hello","brunch","awesome","great","estuary","rationed"]
while guess_word == secret_word:
if len(guess_word) == 2 and len(guess_word) ==3 :
score += 1
elif len(guess_word) == 4:
score += 2
elif len(guess_word) == 5:
score += 3
elif len(guess_word) == 6 and len(guess_word) == 7:
score += 5
elif len(guess_word) == 8:
score += 8
print(f"your score: {score}")
首先,将字符串与列表进行比较。因此,与其这样做
while guess_word == secret_word:
您应该在
if guess_word in secret_word:
其次,如果你像以前那样使用一段时间,它将停留在一个无限循环中
while guess_word == secret_word:
因此,要么像我所做的那样将其更改为if(单次迭代)
或者将guess\u word=input(guess\u word)
放入while-true循环(多次迭代)
这允许guess_word得到更新
最后但并非最不重要的一点是,如果您输入man,您将只获得由以下代码确定的+1分:(顺便说一句,您应该使用or,因为len(guess\u word)==2或len(guess\u word)==3是互斥的)
如果你想根据你给出的分数来增加分数,你应该这样做
score += len(guess_word)
总之,您的代码看起来像:
score = 0
secret_word = ["man","hello","brunch","awesome","great","estuary","rationed"]
while True:
guess_word = input("Guess a word: ")
if len(guess_word) == 2 or len(guess_word) ==3 :
score += len(guess_word)
elif len(guess_word) == 4:
score += len(guess_word)
elif len(guess_word) == 5:
score += len(guess_word)
elif len(guess_word) == 6 and len(guess_word) == 7:
score += len(guess_word)
elif len(guess_word) == 8:
score += len(guess_word)
if guess_word not in secret_word:
break
print("Congrats! Your score is now " + str(score))
print(f"Your Final score: {score}")
条件guess\u word==secret\u word
从来都不是真的:首先,因为guess\u word
是字符串,而secret\u word
是字符串列表。guess\u word
来自input
,它是字符串secret\u word
是字符串列表guess\u word==secret\u word
将始终为false。你的循环从未运行过。我需要一些澄清。你是如何根据长度或其他因素在man和hello中赋值的?如果它认为长度hello length是5但你给了4?谢谢你的建议,我肯定需要一些练习来做,道具给你!!!我添加了score+=score,因此它添加了total@JonathanRimin在我看来,你不需要变量total,所以我放弃了它,用score替换了它
if len(guess_word) == 2 or len(guess_word) ==3 :
score += 1
score += len(guess_word)
score = 0
secret_word = ["man","hello","brunch","awesome","great","estuary","rationed"]
while True:
guess_word = input("Guess a word: ")
if len(guess_word) == 2 or len(guess_word) ==3 :
score += len(guess_word)
elif len(guess_word) == 4:
score += len(guess_word)
elif len(guess_word) == 5:
score += len(guess_word)
elif len(guess_word) == 6 and len(guess_word) == 7:
score += len(guess_word)
elif len(guess_word) == 8:
score += len(guess_word)
if guess_word not in secret_word:
break
print("Congrats! Your score is now " + str(score))
print(f"Your Final score: {score}")