我在比赛中得不到积分

2024-09-27 07:33:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做一个猜谜游戏,我不能让分数说什么,除了0

我在谷歌上搜索了一下,发现一个公式不起作用,所以我来了

为了防止代码看起来很奇怪,这里有一个链接: http://www.codeskulptor.org/#user46_Rn6mbqc2fxzXniV.py

import random

totalScore = 0


lp = input ('Lets play the guessing game! \nPress Enter to start')
if (lp== ""):
    print ("Score\n_____")

x1 = int(input("No.1 Type a number between 0 and 5"))
if (x1 !=(random.randint(0,5))):
    input("Incorrect   :/\n Press Enter to move on")
    print ("+0")
    score = totalScore + 0
else:
    input("Correct!\n Press Enter to move on")
    print ("+1")
x2 = int(input("No.2 Type a number between 0 and 10"))
if (x2 !=(random.randint(0,10))):
    input("Incorrect   :/\n Press Enter to move on")
    print ("+0")
    score = totalScore + 0
else:
    input ("Correct!\n Press Enter to move on")
    print ("+1")
    score = totalScore + 1

x3 = int(input("No.1 Type a number between 0 and 15"))
if (x3 !=(random.randint(0,15))):
    input("Incorrect   :/\n Press Enter to move on")
    print ("+0")
    score = totalScore + 0
else:
    input ("Correct!\n Press Enter to move on")
    print ("+1")
    score = totalScore + 1

x4 = int(input("No.1 Type a number between 0 and 20"))
if (x4 !=(random.randint(0,20))):
    input("Incorrect   :/\n Press Enter to move on")
    print ("+0")
    score = totalScore + 0
else:
    input ("Correct!\n Press Enter to move on")
    print ("+1")
    score = totalScore + 1

x5 = int(input("No.1 Type a number between 0 and 25"))
if (x5 !=(random.randint(0,25))):
    input("Incorrect   :/\n Press Enter to move on")
    print ("+0")
    score = totalScore + 0
else:
    input ("Correct!\n Press Enter to move on")
    print ("+1")
    score = totalScore + 1

totalScore = score

print ("Total Score:")
print (totalScore)

我正在努力让比赛增加所有的积分,并在最后展示他们


Tags: tonumberinputmoveifontyperandom
1条回答
网友
1楼 · 发布于 2024-09-27 07:33:30

每次将score设置为0时,都会覆盖上一个score。你想要做的不是score = totalScore + 1(或0),而是totalScore += 1(与totalScore = totalScore + 1相同)

import random

totalScore = 0


lp = input ('Lets play the guessing game! \nPress Enter to start')
if (lp== ""):
    print ("Score\n_____")

x1 = int(input("No.1 Type a number between 0 and 5"))
if (x1 !=(random.randint(0,5))):
    input("Incorrect   :/\n Press Enter to move on")
    print ("+0")
else:
    input("Correct!\n Press Enter to move on")
    print ("+1")
    totalScore += 1
x2 = int(input("No.2 Type a number between 0 and 10"))
if (x2 !=(random.randint(0,10))):
    input("Incorrect   :/\n Press Enter to move on")
    print ("+0")
else:
    input ("Correct!\n Press Enter to move on")
    print ("+1")
    totalScore += 1

x3 = int(input("No.1 Type a number between 0 and 15"))
if (x3 !=(random.randint(0,15))):
    input("Incorrect   :/\n Press Enter to move on")
    print ("+0")
else:
    input ("Correct!\n Press Enter to move on")
    print ("+1")
    totalScore += 1

x4 = int(input("No.1 Type a number between 0 and 20"))
if (x4 !=(random.randint(0,20))):
    input("Incorrect   :/\n Press Enter to move on")
    print ("+0")
else:
    input ("Correct!\n Press Enter to move on")
    print ("+1")
    totalScore += 1

x5 = int(input("No.1 Type a number between 0 and 25"))
if (x5 !=(random.randint(0,25))):
    input("Incorrect   :/\n Press Enter to move on")
    print ("+0")
else:
    input ("Correct!\n Press Enter to move on")
    print ("+1")
    totalScore += 1

print ("Total Score:")
print (totalScore)

输出示例:

Score
_____
+1
+0
+1
+0
+0
Total Score:
2

在上面的示例中,我还删除了多余的score = totalScore + 0,因为将0添加到整数中没有任何作用

感谢codeskulptor链接使使用代码变得超级简单

相关问题 更多 >

    热门问题