每个排名一遍又一遍地重复
Each rank repeats itself over and over again
我希望在每次循环 运行 时继续执行 totalxp 并将其加在一起,但是我希望 运行k 1 在每次达到更高 运行 时停止打印k.
import math
totalxp = 0
rank = 1
while totalxp < 2000:
xpearned = int(input('Enter xp earned last game'))
totalxp = xpearned + totalxp
if totalxp >= 100:
rank = 1
print('You have been promoted to Corporal', rank)
totalxp = totalxp - 100
print(totalxp)
if totalxp >= 300:
rank = rank + 1
print('You have been promoted to Sergeant', rank)
totalxp = totalxp - 300
if totalxp >= 700:
rank = rank + 2
print('You have been promoted to Staff Sergeant', rank)
totalxp = totalxp - 700
if totalxp >= 1500:
print('You have been promoted to Warrant Officer', rank)
print(totalxp)
发生这种情况的原因是,如果一个值大于 300,则它必须大于 100,并且将执行第一个 if 语句。为避免这种情况,只需将检查较高值的 if 语句放在代码上方,将检查较小值的 if 语句放在下方。另外,对其他 if 块使用 elif。
import math
totalxp = 0
rank = 1
while totalxp < 2000:
xpearned = int(input('Enter xp earned last game'))
totalxp = xpearned + totalxp
if totalxp >= 1500:
print('You have been promoted to Warrant Officer', rank)
elif totalxp >= 700:
rank = rank + 2
print('You have been promoted to Staff Sergeant', rank)
totalxp = totalxp - 700
elif totalxp >= 300:
rank = rank + 1
print('You have been promoted to Sergeant', rank)
totalxp = totalxp - 300
elif totalxp >= 100:
rank = 1
print('You have been promoted to Corporal', rank)
totalxp = totalxp - 100
print(totalxp)
我希望在每次循环 运行 时继续执行 totalxp 并将其加在一起,但是我希望 运行k 1 在每次达到更高 运行 时停止打印k.
import math
totalxp = 0
rank = 1
while totalxp < 2000:
xpearned = int(input('Enter xp earned last game'))
totalxp = xpearned + totalxp
if totalxp >= 100:
rank = 1
print('You have been promoted to Corporal', rank)
totalxp = totalxp - 100
print(totalxp)
if totalxp >= 300:
rank = rank + 1
print('You have been promoted to Sergeant', rank)
totalxp = totalxp - 300
if totalxp >= 700:
rank = rank + 2
print('You have been promoted to Staff Sergeant', rank)
totalxp = totalxp - 700
if totalxp >= 1500:
print('You have been promoted to Warrant Officer', rank)
print(totalxp)
发生这种情况的原因是,如果一个值大于 300,则它必须大于 100,并且将执行第一个 if 语句。为避免这种情况,只需将检查较高值的 if 语句放在代码上方,将检查较小值的 if 语句放在下方。另外,对其他 if 块使用 elif。
import math
totalxp = 0
rank = 1
while totalxp < 2000:
xpearned = int(input('Enter xp earned last game'))
totalxp = xpearned + totalxp
if totalxp >= 1500:
print('You have been promoted to Warrant Officer', rank)
elif totalxp >= 700:
rank = rank + 2
print('You have been promoted to Staff Sergeant', rank)
totalxp = totalxp - 700
elif totalxp >= 300:
rank = rank + 1
print('You have been promoted to Sergeant', rank)
totalxp = totalxp - 300
elif totalxp >= 100:
rank = 1
print('You have been promoted to Corporal', rank)
totalxp = totalxp - 100
print(totalxp)