Python 询问用户是否想再次播放-我键入“时,代码仍会重复”;";
这是我的密码:Python 询问用户是否想再次播放-我键入“时,代码仍会重复”;";,python,Python,这是我的密码: play = True while play: #1 A company gives bonus of 5% to employees with year of service of more than 5 years. salary = float(input("Insert your salary ")) bonus = float((salary * 0.05) + salary) yearOfService = int(input("In
play = True
while play:
#1 A company gives bonus of 5% to employees with year of service of more than 5 years.
salary = float(input("Insert your salary "))
bonus = float((salary * 0.05) + salary)
yearOfService = int(input("Insert how many years you have worked "))
if yearOfService > 5:
print ("You now have $" + str(round(bonus,2)))
else:
print ("Sorry, but you need to have more than 5 years of service for a bonus.")
#2 A school's grading system.
score = float(input("Enter your score: "))
print("Your score is " + str(score))
grade = ""
if score < 25:
grade = "F"
if score >= 25 and score <= 45:
grade = "E"
if score >= 45 and score <=50:
grade = "D"
if score >= 50 and score <= 60:
grade = "C"
if score >= 60 and score <= 80:
grade = "B"
if score > 80:
grade = "A"
print("Your grade is " + grade)
#3 Weird/Not Weird
for i in range(0,3):
n = float(input("Enter integer: "))
if n%2 == 1:
print "Weird"
if n%2 == 0 and n <= 2 and n >= 5:
print "Not Weird"
if n%2 == 0 and n <= 6 and n >= 20:
print "Weird"
if n%2 == 0 and n > 20:
print "Not Weird"
#4. In a given string "mississippi" count number of 's' and 'i'
string = "mississippi"
sCount = 0
iCount = 0
for i in string:
if i == 's':
sCount = sCount + 1
if i == 'i':
iCount = iCount + 1
print( str(sCount) + " s's and " + str(iCount) + "i's")
#5. Print "Hello World" 8 times
f = 1
while f <= 8:
print("Hello World")
f += 1
again = input("Would you like to play again?")
if again == "n":
play = False
play=True
游戏时:
#1公司向服务年限超过5年的员工发放5%的奖金。
工资=浮动(输入(“插入您的工资”))
奖金=浮动((工资*0.05)+工资)
yearOfService=int(输入(“插入您工作了多少年”))
如果服务年限>5年:
打印(“您现在有$”+str(一轮(奖金,2)))
其他:
打印(“对不起,您需要5年以上的服务才能获得奖金。”)
#2学校的评分系统。
分数=浮动(输入(“输入您的分数:”)
打印(“您的分数为”+str(分数))
grade=“”
如果分数<25:
grade=“F”
如果得分>=25,得分=45,得分=50,得分=60,得分80:
等级=“A”
打印(“你的成绩是”+成绩)
#3奇怪/不奇怪
对于范围(0,3)内的i:
n=浮点(输入(“输入整数:”)
如果n%2==1:
打印“怪异”
如果n%2==0且n=5:
打印“不奇怪”
如果n%2==0且n=20:
打印“怪异”
如果n%2==0且n>20:
打印“不奇怪”
#4.在给定字符串“mississippi”中,计算“s”和“i”的数量
string=“密西西比”
Scont=0
i计数=0
对于字符串中的i:
如果i=='s':
童子军=童子军+1
如果i==“i”:
iCount=iCount+1
打印(str(scont)+“s”和“+str(iCount)+“i”)
#5.打印“Hello World”8次
f=1
哦,这是一个有趣的故事
首先我们需要看第36行:
print "Weird"
因为这没有导致错误,这意味着代码正在Python2.x下运行。(从代码的其余部分可以清楚地看出,您打算使用Python的3.x版本,这会导致语法错误。)
在2.x中,input
将输入文本解释为当前上下文中的Python表达式;这相当于在输入文本上调用eval()
。(您不想这样做。)
发生的情况是,因为您在程序的前面有一个变量n
,所以您键入的文本n
将计算为该变量的值(即,以前的奇怪或不奇怪的数字)。(例如,如果您改为键入了y
,则会引发异常。)
解决方案:您需要修复print
函数调用,然后确保代码使用Python 3.x解释器。退出游戏时是否键入n或“n”?您的问题只是循环重复;为什么其他代码都在这里?另外,不要期望我们提供输入:您可以使用硬编码的变量进行输入。键入n并按enter键为我退出游戏。@user2263572在此尝试输出您的值,如print(“再次:%s.%再次)
,并且此如果再次==“n”:play=False
可以替换为play=reach!='啊,哈!这就是为什么我刚才在尝试诊断时出现了float
错误的原因。或者,如果您出于某种原因使用Python 2.x,raw\u input
是您想要使用的函数。因为Python 2.x,我是在假设没有新程序员有合法理由使用它的情况下工作的。我在一个Python2.x程序上工作,我在Python3程序上试用了它来纠正打印函数调用,它成功了,谢谢!