简体   繁体   English

如何阻止我的代码打印额外的问题?

[英]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.

相关问题 如果有人回答错误,我该如何停止我的代码? - How do I stop my code if someone answers a question wrong? 如何阻止我的代码无限打印“拒绝访问”? - How do I stop my code from infinitely printing "Access Denied"? Python 如何阻止代码打印 output - Python How do I stop the code from printing output 所以,基本上我的代码在打印我希望它打印的语句后打印 None 。 我怎样才能阻止这个 None 打印 - So, basically my code is printing None after printing the statement I want it to print. How can I stop this None from printing 如何阻止我的 Python if 语句与我的 else 语句一起打印? - How do I stop my Python if statement from printing with my else statement? 在 Python 上没有询问任何问题时,如何阻止我的 Magic 8 Ball 程序生成随机数? - How do I stop my Magic 8 Ball Program from generating a random number when no question is asked on Python? 如何在最后一个字母后停止打印点? - How do I stop the dot from printing after the last letter? 如何阻止每个字母打印在不同的行上? - How do I stop each letter from printing on different line? 我如何让我的代码在没有额外的 &#39;&#39; 和 () 的情况下返回答案 - How do i get my code to return the the answer without the extra '' and () 如何获取我的代码以停止在我的网络搜寻器中打印关键字 - How to get my code to stop printing a keyword in my web crawler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM