#๐Ÿ”’ how can i improve my code

8 messages ยท Page 1 of 1 (latest)

wary scarab
#

while True:
    answer = input("Roll the dice? (y/n): ")

    if answer.lower() == "y":
        r = (random.randint(1, 6), random.randint(1, 6))
        print(r)
    elif answer.lower() == "n":
        print("Thanks for playing")
        quit()
    else:
        print("Enter a valid answer")```
coarse thicketBOT
#

Hey @wary scarab!

Please edit your message to use a code block

Add a py after the three backticks.

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
coarse thicketBOT
#

@wary scarab

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

high slate
# wary scarab ```import random while True: answer = input("Roll the dice? (y/n): ") ...

There's not much code here, so there isn't much that can be commented on. A couple of things, though:

  • This code can only generate two die at a time. Try to allow the user to pick how many die to roll each round (hint: you'll want a list and a loop)
  • Try to incorporate your own functions. Once you do the above suggestion, make the "generate dice" logic its own separate function.
#
  • Instead of calling quit, you can instead just break from the loop. That will allow this logic to be more generally useful, since killing the entire program won't be a necessity.
#
  • Allow the user to pick how many sides the die has instead of forcing a d6
coarse thicketBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.