使用换行符(n)

  • 本文关键字:换行符 python
  • 更新时间 :
  • 英文 :


我有一个while循环,下面是代码的一部分。完整代码的截图

在检查我的工作时,我注意到当它打印出noRulesQuestion时,它没有使用新行。这里是输出,从屏幕截图显示我已经使用分割代码,而不是有一长行文本和n显示,同时也没有采取新的行。如果我要这样做,它将工作示例:noRulesQuestion = print(输出的屏幕截图如果我要这样做

noRulesQuestion = ("Each player rolls two dice. If the scores on the two dice are different, they are added. If they are the same then the score is increased be 50% (so two 3s would score a 9).", "n"
"If one of the scores is greater than 12 the other player automatically wins.", "n" 
"If one of the scores is equal to twelve, that player wins.")
while True:  
rulesQuestion = input("Do you know the rules? Type Yes or No: ")    
if rulesQuestion == "No" and "no":
print(noRulesQuestion)      
break

有办法解决这个问题吗?我对编程很陌生,似乎不太懂。

编辑:更新了我格式化问题的方式,谢谢你的回复!

您的noRulesQuestion变量是一个字符串元组。如果您不知道什么是元组,请查看此链接。要打印出以新行字符分隔的所有规则,您可以这样做:

rules = ["Each player rolls two dice. If the scores on the two dice are different, they are added. If they are the same then the score is increased be 50% (so two 3s would score a 9).", "If one of the scores is greater than 12 the other player automatically wins.", "If one of the scores is equal to twelve, that player wins."]
while True:
rulesQuestion = input("Do you know the rules? Type Yes or No: ")
if rulesQuestion == "No" and "no":
for rule in rules: print(rule)
break

同样,如果你想验证用户的输入是"No"还是"no",你应该这样显式:

rules = ["Each player rolls two dice. If the scores on the two dice are different, they are added. If they are the same then the score is increased be 50% (so two 3s would score a 9).", "If one of the scores is greater than 12 the other player automatically wins.", "If one of the scores is equal to twelve, that player wins."]
while True:
rulesQuestion = input("Do you know the rules? Type Yes or No: ")
if rulesQuestion == "No" or rulesQuestion == "no":
for rule in rules: print(rule)
break

最新更新