如果用户为scoreEarned输入90,为scoreShift输入20,则需要找到一个不允许用户输入超过100的等式,



我被困在elif语句上,我不知道我如何能够让输入例如scoreEarned = 90和scoreShift =20,这等于110,只是100作为输出的最大值。就像玩家能够获得的最高分是100分,但如果玩家获得了超过100分的额外分数,它怎么还能返回100作为最高分呢?

#Message display description
print("This program reads exam/homework scores and reports your overall course grade.")
#Space
print()
#Title for Midterm 1
print("Midterm 1:")
#funtion that asks user for input to use
def totalPoint():
#Asking user for weight of assignment
weight = int(input("Weight (0-100)? "))
#Asking user for score earned on assignment, max score is 100
scoreEarned = int(input("Score earned? "))
#variable for bottom half of fraction
outOf = 100
#prompting user to input 1 for yes and 2 for no
yesOrNo = int(input("Were scores shifted (1=yes, 2=no)? "))
#if statement for if there is a score shift
if(yesOrNo == 1):
#prompt user for shift amount
scoreShift = int(input("Shift amount? "))
#if statement that will check user input
if(scoreShift > 0 and scoreShift + scoreEarned <= 100):

#add score earned and score shift
newTotalPoint = scoreEarned + scoreShift
#display new total points
print("Total points = ",newTotalPoint,"/",outOf)
#elif statement if the combined total of the score shift and earned are more than 100
elif(scoreShift > 0 and scoreShift + scoreEarned > 100):
#Need to have a maximum of 100 for total points including the shift
print("This is where I am stuck.")
#else there is no shift score
else:
#Print the  score earned out of 100
print("Total points = ",scoreEarned,"/",outOf)

totalPoint()

print(100).
像这样写:

elif(scoreShift > 0 and scoreShift + scoreEarned > 100):
#Need to have a maximum of 100 for total points including the shift
print("Total points = 100")

另一种更合理/更python的方法是使用python内置的min()函数,如下所示

scoreEarned = min(100, scoreEarned + scoreShift)
print("Total points = ",scoreEarned,"/",outOf)

min将返回传递给它的两个参数中较小的那个。如果scoreEarned + scoreShift大于您的上限值100,min将返回100,否则将返回原始总和。

在此阶段将print("This is where I am stuck.")替换为重要的逻辑输出print("You exceeded the maximum score and will receive 100 points ")

这是我从你的问题中推断出来的。这就是你想做的,对吧?

最新更新