Ask coding questions

← Back to all posts
Lucky Sevens
mikebra2003 (0)

Hi, i'm really new to python but what do you think about this? I know it's nothing but i just wanna know if i wrote all in the right way.

Answered by Coder100 (17034) [earned 5 cycles]
View Answer
Coder100 (17034)

It's good, but a more better code would be:

import random

print("Welcome to Lucky Sevens")
print("Try your luck by getting seven!\n\n")
attempts = 5
while attempts > 0:
  start = input("Press a button to roll the dice...")
  attempts -= 1
  num1 = random.randint(1, 6 + 1)
  num2 = random.randint(1, 6 + 1)
  sum = num1 + num2
  print("Number 1:", num1,"\nNumber 2:", num2)
  print("Sum:", sum)
  if sum == 7:
    print("You won with", attempts, "attempt left.")
    print("Unlucky, you should try again...")
    print(attempts, "attempts left.\n")

everything seems to be working.

IntellectualGuy (693)

It seems to be working fine but in the randint I suggest doing randint(1,7) instead of randint(1, 6 + 1), they're both the same thing. Also you don't need paranthesis around sum == 7, just leave it like it is.

Coder100 (17034)

perhaps they wanted to emphasize the exclusiveness @IntellectualGuy