1

I've got a while loop going, down below is part of the code. Screenshot of the full code

While checking over my work I noticed that it wasn't taking a new line when it prints out noRulesQuestion. Here is the output, from the screenshot the , shows that I've used to split the code instead of having a long line of text and the \n shows while also not taking a new line. It would work if I were to do example: noRulesQuestion = print( Screenshot of the output if I were to do this

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

Is there a way to fix this? I'm quite new to coding and can't seem to get my head round it.

Edit: Updated the way I've formatted the question, thank you for the replies!

jxc
  • 11
  • 2
  • 1
    1) Please fix the formatting (you can put triple-backquotes, `\`\`\``, around blocks of code to get syntax-highlighting and proper formatting). The indentation needs fixing too. 2) I'm not even sure what the problem is, your description is vague to the point of inscrutability, and your code doesn't constitute a [MCVE]. – ShadowRanger Jul 06 '22 at 21:22
  • 1
    `if rulesQuestion == "No" and "no":` is not the way to test for multiple possibilities. It should be `if rulesQuestion in ("No", "no"):` – Barmar Jul 06 '22 at 21:23
  • Side-note: You almost certainly want `if rulesQuestion in ("No", "no"):`; what you've written is equivalent to `if rulesQuestion == "No":` (because `and "no"` is just checking if `"no"` is a non-empty string, not checking it against `rulesQuestion` at all, and since `"no"` is always non-empty, it's meaningless, only the first test ends up mattering). – ShadowRanger Jul 06 '22 at 21:24
  • 1
    @Mozway That duplicate is not what his question is about, it's an unrelated problem in the code. – Barmar Jul 06 '22 at 21:25
  • Oh thanks @Barmar I see this one so often, I was too quick to judge – mozway Jul 06 '22 at 21:27
  • Are you asking how to print a list of strings with newline between them? – Barmar Jul 06 '22 at 21:28
  • I'm sure there are duplicates, I can't find one at the moment. It's `print(*noRulesQuestion, sep='\n')` – Barmar Jul 06 '22 at 21:30
  • @ShadowRanger Sorry, I've updated the description of the question. Hopefully now it is more clear to understand. Thank you for the explanation for the if rulesQuestion bit. Another user has written *if rulesQuestion == "No" or rulesQuestion == "no":*. Which one is the better way to write the rule? Can you kindly explain it to me. Once again thank you! – jxc Jul 07 '22 at 08:05
  • @Barmar Yea I was asking to print a list of strings with newline between them, it seems akaHenry comment worked. I did not test out your solution of *print(*noRulesQuestion, sep='\n')*. Would your solution be better than what akaHenry done? Can you please kindly explain if so. – jxc Jul 07 '22 at 08:12
  • They're both equivalent, it's just programming style. – Barmar Jul 07 '22 at 14:02

1 Answers1

0

Your noRulesQuestion variable is a tuple of strings. If you don't know what is a tuple, take a look at this link. To print out every rule separated by a new line character, you can do something like this:

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

Also, if you want to verify if the user's input was "No" or "no", you should explicit it like this:

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
akahenry
  • 1
  • 3
  • Can you explain why *for rule in rules: print(rule)* and not just like print(rules) – jxc Jul 07 '22 at 08:16
  • @jxc: `print(rules)` is printing the `tuple` itself, which displays the `repr` of its contents (so `\n` is displayed as an actual backslash followed by `n`, not a newline). The loop pulls out each string and prints them individually, which prints the "human-friendly" form (as opposed to developer-friendly `repr`), without quotes, and with newlines as actual newlines. `print(*rules, sep="\n")` would get the same effect, by unpacking, as if you'd called `print(rules[0], rules[1], ..., sep="\n")`, printing each string individually with newlines between them. – ShadowRanger Jul 07 '22 at 10:23