0

I let the user input a 3-digit number, and let python throw a random 3-digit number, try to see when the random number can match the input. However, everytime I run it on cmd, it prints 'Python is too tired', which is a statement I set to prevent it from looping infinitely (I really did so, until I added this limitation of attempt times).

import random

my_num = int(input('Type your 3-digit int number: '))

if my_num >= 100 and my_num < 1000:
    attempt = 0

    my_lottery = []
    my_lottery.append(int(num) for num in str(my_num))

    def lottery_throw():
        lottery = []
        i=0
        while i<3:
            lottery.append(random.randint(1,9))
            i+=1
            return(lottery)

    def check():
        global boo
        lottery_throw()
        if lottery == my_lottery:
            boo = 'true'
        else:
            boo = 'false'

    while attempt < 100000:
        check()
        attempt += 1
        if boo == 'true':
            print('you win')
            print(attempt)
            break
        elif attempt >= 100000:
            print('python is too tired')
            break

else:
    print('You must enter a 3-digit number.')

Run it on cmd, everytime I run it, it returns 'Python is too tired'

But the number of possible combinations (9C3) is only 84. It's very unlikely that python really didn't throw a correct number. How can I fix this problem?

LIdbioe
  • 7
  • 3
  • When you debugged this, what did you find is the value of `boo` in the line `if boo == 'true'`? – mkrieger1 Nov 30 '20 at 12:29
  • @Nobody `9c3` is 84, but it is not the number of posible lottery tickets, it would be `9^3` as you are not allowing zeros (which by the way is a mistake) with is 729. Still there are other errors listed in my answer but just wanted to know that `9c3` has no meaning here. – Adirio Nov 30 '20 at 13:05

2 Answers2

1

Errors

  • Do not use global variables unless you really need them. As you can see, writting to a global variable doesn't work our of the box
  • 012 is a valid 3 digit number
  • If you append a list to another list you will get a nested list (list inside another list): [[1, 2, 3]]
  • Why create a list of each digit when you can create the number itself?
  • for loops should be used for a fixed number of iterations, while loops when a more complex condition is needed to return from the loop.
  • return should be used without parenthesis as they are redundant (it is not a function)
  • True and False are booleans, do not use "true" and "false" (strings) to represent them.
  • if condition return true else return false (in pseudocode) is the same as return condition
  • for loops also can have an else clause, which only is executed if no break was found.

Solution

import random

my_num = int(input("Type your 3-digit int number: "))


if 0 <= my_num < 1000:
    for attempt in range(1, 100001):
        if my_num == random.randint(0, 999):
            print(f"You win in {attempt} attempts.")
            break
    else:
        print("Python is too tired.")

else:
    print("You must enter a 3-digit number.")
Adirio
  • 5,040
  • 1
  • 14
  • 26
-2

I have no idea what you did, but I wasn't able to fix it.

Here is my version incited:

num = input('Type your 3-digit int number: ')
for i in range(100000):
    if (("00"+str(random.randint(0,999)))[-3:]) == num:
        print("You win on attempt %i"%i)
        did_win = True
        break
else:print('python is too tired')
Minek Po1
  • 142
  • 1
  • 9