[英]How do I stop my code from printing the extra question?
How do I stop from printing an extra input line?如何停止打印额外的输入行? I'm new with python/coding
我是 python/编码的新手
class1 = "Math"
class2 = "English"
class3 = "PE"
class4 = "Science"
class5 = "Art"
def get_input(className):
classInput = raw_input("Enter the score you received for " + className + ": ")
while int(classInput) >= 101 or int(classInput) <= -1:
print "Needs to be in the range 0 to 100"
classInput = raw_input("Enter the score you received for " + className + ": ")
return int(classInput)
def get_letter_grade(grade):
if grade >= 93:
return"A"
elif grade >= 90:
return"A-"
elif grade >= 87:
return"B+"
elif grade >= 83:
return"B"
elif grade >= 80:
return"B-"
elif grade >= 77:
return"C+"
elif grade >= 73:
return"C"
elif grade >= 70:
return"C-"
elif grade >= 67:
return"D+"
elif grade >= 63:
return"D"
elif grade >= 60:
return"D-"
else:
return"F"
print "Your " + class1 + " score is " + str(get_input(class1)) + ", you got a " +
get_letter_grade(get_input(class1))
Prints out:打印出来:
Enter the score you received for Math: 85
Enter the score you received for Math: 85
Your Math score is 85, you got a B
Inside your print, you call get_input()
method twice:在您的打印中,您调用
get_input()
方法两次:
print "Your " + class1 + " score is " + str(get_input(class1)) + ", you got a " +
get_letter_grade(get_input(class1))
What you need to do is store your score by calling get_input()
method once and use the stored value in print method:您需要做的是通过调用一次
get_input()
方法来存储您的分数,并在 print 方法中使用存储的值:
score = get_input(class1)
print("Your " + class1 + " score is " + str(score) + ", you got a " +
get_letter_grade(score))
I would separate out your calls to get_input
from your print statement, not just here, but generally.我会将您对
get_input
的调用与您的 print 语句分开,不仅在这里,而且通常是这样。
score = str(get_input(class1))
print "Your " + class1 + " score is " + score + ", you got a " +
get_letter_grade(score)
As a rule of thumb, any user input should almost always be immediately stored in a variable to be manipulated and/or used later.根据经验,任何用户输入几乎总是应该立即存储在一个变量中,以便以后操作和/或使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.