【问题标题】:Why this python code is not executing completely?为什么这个 python 代码没有完全执行?
【发布时间】:2020-04-21 07:56:32
【问题描述】:

当我在 pycharm 中运行以下代码时,它会一直执行到 ans = input("Do you want to play this game") 并且当我输入答案是时,它应该理想地执行下面的代码,但它会执行 print('Thank you玩了这么多,你得到了',得分,'问题正确')

print ("Welcome to the page?")

ans = input("Do you want to play this game? Yes/No")
score = 0

if ans.lower()=='yes':
   ans=input("When was Bangalore made capital city of Karnataka?")
   if ans.lower()=="1956":
      score=+1
      print("Correct + 1")
   else:
      print("In-correct")

   ans=input("Who is the current Mayor of Bangalore?")
   if ans.lower()=="M Goutham Kumar":
      score=+1
      print("Correct + 1")
   else:
      print("In-correct")

   ans=input("What is the GDP of Bangalore?")
   if ans.lower()=="$210 Billion":
      score=+1
      print("Correct + 1")
   else:
      print("In-correct")

   ans = input("Who is called as father of Bangalore?")
   if ans.lower() == "Kempegowda":
      score = +1
      print("Correct + 1")
   else:
      print("In-correct")

print('Thank you so much for playing, you got', score, 'questions correct')

【问题讨论】:

  • 这对我有用!你能检查变量ans吗?
  • 这行得通,我尝试将Yesyes 作为输入。

标签: python pycharm python-3.6


【解决方案1】:

实际上,您的代码正在运行。 但是,也许您在单词之后或之前输入了一些空格。 或者,也许在 Linux 上,有输入 ans\r

print ("Welcome to the page?")

ans = input("Do you want to play this game? Yes/No ")
score = 0

if ans.lower().strip()=='yes':
   ans=input("When was Bangalore made capital city of Karnataka? ")
   if int(ans)==1956:
      score=+1
      print("Correct + 1")
   else:
      print("In-correct")

   ans=input("Who is the current Mayor of Bangalore? ")
   if ans.lower().strip()=="m goutham kumar":
      score=+1
      print("Correct + 1")
   else:
      print("In-correct")

   ans=input("What is the GDP of Bangalore? ")
   if ans.lower().strip()=="$210 billion":
      score=+1
      print("Correct + 1")
   else:
      print("In-correct")

   ans = input("Who is called as father of Bangalore? ")
   if ans.lower().strip() == "Kempegowda":
      score = +1
      print("Correct + 1")
   else:
      print("In-correct")

print('Thank you so much for playing, you got', score, 'questions correct')

【讨论】:

  • 是的,空格是错误的。谢谢 :) 但现在对于第 2、3 和第 4 个问题,即使我输入了准确的单词,我得到的答案也是不正确的
  • 您应该尝试ans.lower()=="m goutham kumar",而不是单词中的大写字母。
  • 也许这个答案可以专注于相关的代码行,而不是重复所有内容
【解决方案2】:
print ("Welcome to the page?")

ans = input("Do you want to play this game? Yes/No")


score = 0

if ans.lower()=='yes':

   ans=input("When was Bangalore made capital city of Karnataka?")

   if ans.lower()=="1956":

      score=+1

      print("Correct + 1")

   else:

      print("In-correct")

   ans=input("Who is the current Mayor of Bangalore?")

   if ans.lower()=="M Goutham Kumar":

      score=+1

      print("Correct + 1")

   else:

      print("In-correct")

   ans=input("What is the GDP of Bangalore?")

   if ans.lower()=="$210 Billion":

      score=+1

      print("Correct + 1")

   else:

      print("In-correct")

   ans = input("Who is called as father of Bangalore?")

   if ans.lower() == "Kempegowda":

      score = +1

      print("Correct + 1")

   else:

      print("In-correct")

print('Thank you so much for playing, you got', score, 
'questions correct')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    • 2013-06-26
    相关资源
    最近更新 更多