Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 same number everytime when using random.randint
#1
Every time I run this the random number to print out win is 3. Any help with this would be greatly appreciated!

import random
from random import randint
print("hi")
luck = random.randint(1, 3)
print(luck)
loto = (int(input("Pick a number from 1 to 3: ")))
while "luck" != "loto":
    if loto > 0 and loto < 3:
        print("closey")
        luck = random.randint(1, 3)
        loto = (int(input("Pick a number from 1 to 3: ")))
    elif loto <= 0 or loto > 3:
        print("Not closey")
        luck = random.randint(1, 3)
        loto = (int(input("NOOOOOPick a number from 1 to 3: ")))
    else:
        for i in range(8):
            print("WIN" * 10)
        break
Quote
#2
I have got 1 as random number

Output:
hi 1 Pick a number from 1 to 3: 2 closey Pick a number from 1 to 3: 9 Not closey NOOOOOPick a number from 1 to 3: 3 WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN Process finished with exit code 0
Quote
#3
(Aug-23-2019, 03:31 AM)summeringpainting Wrote: Every time I run this the random number to print out win is 3. Any help with this would be greatly appreciated!

import random
from random import randint
print("hi")
luck = random.randint(1, 3)
print(luck)
loto = (int(input("Pick a number from 1 to 3: ")))
while "luck" != "loto":
    if loto > 0 and loto < 3:
        print("closey")
        luck = random.randint(1, 3)
        loto = (int(input("Pick a number from 1 to 3: ")))
    elif loto <= 0 or loto > 3:
        print("Not closey")
        luck = random.randint(1, 3)
        loto = (int(input("NOOOOOPick a number from 1 to 3: ")))
    else:
        for i in range(8):
            print("WIN" * 10)
        break

Hi!

In line 7, you compare two strings: "luck" and "loto", that is to say, you are comparing 2 different words, so you are saying if they are different, which they always will be, then you have to choose from the other options down.

Therefore, in line 8, you are saying if the value of the number introduced by the user (here you asked to check the value of the number, and not checking for words as before) is 1 or 2, the program will always say the message "closey".

In line 12, you exclude numbers less or equal than 0, and numbers bigger than 3.

Therefore, the only number left, else (line 16) is 3, so 3 always gives you WINWINWIN...
even if as in Malt's output, the number ramdonly chosen in the variable named luck, is 1.

(Aug-23-2019, 04:51 AM)Malt Wrote: I have got 1 as random number

Output:
hi 1 Pick a number from 1 to 3: 2 closey Pick a number from 1 to 3: 9 Not closey NOOOOOPick a number from 1 to 3: 3 WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN Process finished with exit code 0

I just made some modifications to your program, and it seems to work just fine now. Notice that I put out of the loop, the option else, because it gave always WIN to 3. Now while the value of the variable luck is different to the variable loto, you have the loop, otherwise (else), it means that the two variables are equal and therefore you win:

import random
from random import randint
print("hi")
luck = random.randint(1, 3)
print(luck)
loto = (int(input("Pick a number from 1 to 3: ")))
while luck != loto:
    if loto > 0 and loto < 3:
        print("closey")
        luck = random.randint(1, 3)
        loto = (int(input("Pick a number from 1 to 3: ")))
    elif loto <= 0 or loto > 3:
        print("Not closey")
        luck = random.randint(1, 3)
        loto = (int(input("NOOOOOPick a number from 1 to 3: ")))
else:
    for i in range(8):
        print("WIN" * 10)

Oops!

While checking some outputs, and reviewing the program, I have noticed there are still some bugs:

Output:
hi 2 Pick a number from 1 to 3: 2 WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN
Output:
hi 1 Pick a number from 1 to 3: 1 WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN
Output:
hi 3 Pick a number from 1 to 3: 3 WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN
Output:
hi 2 Pick a number from 1 to 3: 1 closey Pick a number from 1 to 3: 9 Not closey NOOOOOPick a number from 1 to 3: 3
In your original program:

import random
from random import randint
print("hi")
luck = random.randint(1, 3)
print(luck)
loto = (int(input("Pick a number from 1 to 3: ")))
while "luck" != "loto":
    if loto > 0 and loto < 3:
        print("closey")
        luck = random.randint(1, 3)
        loto = (int(input("Pick a number from 1 to 3: ")))
    elif loto <= 0 or loto > 3:
        print("Not closey")
        luck = random.randint(1, 3)
        loto = (int(input("NOOOOOPick a number from 1 to 3: ")))
    else:
        for i in range(8):
            print("WIN" * 10)
        break
inside the loop, in lines 10 and 14, you have made the program to generate another random number, substituting the original one, which I think it's not the point of the game. Once a number has randomly been generated, at the beginning of the program, you should keep it until it has been correctly guessed.

My bad at quickly having a go at changing the program without having checked it properly.

In line 8 of your original program, being the values different, the number introduced could have been 1, 2 or 3, not only 1 or 2, as your line 8 implies.

So I have made some changes again:
import random
from random import randint
print("hi")
luck = random.randint(1, 3)
print(luck)
loto = (int(input("Pick a number from 1 to 3: ")))
while luck != loto:
    if loto > 0 and loto < 4:
        print("closey")
        loto = (int(input("Pick a number from 1 to 3: ")))
    elif loto <= 0 or loto > 3:
        print("Not closey")
        loto = (int(input("NOOOOO! Pick a number from 1 to 3: ")))
else:
    for i in range(8):
        print("WIN" * 10)
I hope not to have left any more bugs with my sleepy head...
By the way, I guess you are just practising and seeing what your code does, because otherwise, I wouldn't find very interesting a guessing-a-number game, knowing it from the beginning. I mean, you can store the number generated by the program without printing it on the screen. That is to say, I would delete the line
print(luck)
from the real game. Also, keep in mind that, as the program generates a random number among 1, 2 and 3, it's quite probably that the same number could be generated repeated times (laws of probability)!!!

I enclosed some inputs and outputs from running the program included in this answer:

Output:
hi 2 Pick a number from 1 to 3: 2 WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN
Output:
hi 2 Pick a number from 1 to 3: 3 closey Pick a number from 1 to 3: 1 closey Pick a number from 1 to 3: 9 Not closey NOOOOO! Pick a number from 1 to 3: 2 WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN
Output:
hi 1 Pick a number from 1 to 3: 1 WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN
Output:
hi 1 Pick a number from 1 to 3: 0 Not closey NOOOOO! Pick a number from 1 to 3: 1 WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN
Output:
hi 3 Pick a number from 1 to 3: 3 WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN
I wish it helps.
Malt likes this post
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Quote
#4
Thank you so much newbieAuggie2019! I made the changes you have suggested, it works and finally makes sense to me!

(Aug-23-2019, 08:30 AM)newbieAuggie2019 Wrote: By the way, I guess you are just practising and seeing what your code does, because otherwise, I wouldn't find very interesting a guessing-a-number game, knowing it from the beginning. I mean, you can store the number generated by the program without printing it on the screen. That is to say, I would delete the line
print(luck)
from the real game.

I put it on line 5 to see if the code was checking that number. I wasn't sure if the code chose a different number every time it was run. Now that I know it does stay the same, I decided to take lines 11 and 15 out so it didn't change the number as you suggested.

Thanks again!
Quote
#5
(Aug-23-2019, 09:53 AM)summeringpainting Wrote: Thank you so much newbieAuggie2019! I made the changes you have suggested, it works and finally makes sense to me!

(Aug-23-2019, 08:30 AM)newbieAuggie2019 Wrote: By the way, I guess you are just practising and seeing what your code does, because otherwise, I wouldn't find very interesting a guessing-a-number game, knowing it from the beginning. I mean, you can store the number generated by the program without printing it on the screen. That is to say, I would delete the line
print(luck)
from the real game.

I put it on line 5 to see if the code was checking that number. I wasn't sure if the code chose a different number every time it was run. Now that I know it does stay the same, I decided to take lines 11 and 15 out so it didn't change the number as you suggested.

Thanks again!

I thought so, I also do it to see if everything is working...

You are welcome! Big Grin

By the way, you are saying you took out lines 11 and 15. According to the line numeration as appeared here on your original post, the lines to be taken out are 10 and 14, the ones that say:

        luck = random.randint(1, 3)
Probably at home you have an additional blank line or a comment that makes there the lines 10 and 14 to appear as 11 and 15, but I just wanted to clarify it, just in case...

I'm also learning, and I like to start the easy way and to add other things bit by bit. For instance, you could add now a counter, and make the program tell you something of the sort of: "You are the best! You guessed the number in just "X" tries!", while having fun. Of course, for that, I would change the range to generate a random number from 1 to 20, or from 1 to 50, or from 1 to 100. Even, later on, you could make that a choice of difficulty of the game like easy, medium and difficult. Anything as you are having fun and learning.

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Quote
#6
(Aug-23-2019, 08:30 AM)newbieAuggie2019 Wrote:
(Aug-23-2019, 03:31 AM)summeringpainting Wrote: Every time I run this the random number to print out win is 3. Any help with this would be greatly appreciated!
import random from random import randint print("hi") luck = random.randint(1, 3) print(luck) loto = (int(input("Pick a number from 1 to 3: "))) while "luck" != "loto": if loto > 0 and loto < 3: print("closey") luck = random.randint(1, 3) loto = (int(input("Pick a number from 1 to 3: "))) elif loto <= 0 or loto > 3: print("Not closey") luck = random.randint(1, 3) loto = (int(input("NOOOOOPick a number from 1 to 3: "))) else: for i in range(8): print("WIN" * 10) break 
Hi! In line 7, you compare two strings: "luck" and "loto", that is to say, you are comparing 2 different words, so you are saying if they are different, which they always will be, then you have to choose from the other options down. Therefore, in line 8, you are saying if the value of the number introduced by the user (here you asked to check the value of the number, and not checking for words as before) is 1 or 2, the program will always say the message "closey". In line 12, you exclude numbers less or equal than 0, and numbers bigger than 3. Therefore, the only number left, else (line 16) is 3, so 3 always gives you WINWINWIN... even if as in Malt's output, the number ramdonly chosen in the variable named luck, is 1.
(Aug-23-2019, 04:51 AM)Malt Wrote: I have got 1 as random number
Output:
hi 1 Pick a number from 1 to 3: 2 closey Pick a number from 1 to 3: 9 Not closey NOOOOOPick a number from 1 to 3: 3 WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN Process finished with exit code 0
I just made some modifications to your program, and it seems to work just fine now. Notice that I put out of the loop, the option else, because it gave always WIN to 3. Now while the value of the variable luck is different to the variable loto, you have the loop, otherwise (else), it means that the two variables are equal and therefore you win:
import random from random import randint print("hi") luck = random.randint(1, 3) print(luck) loto = (int(input("Pick a number from 1 to 3: "))) while luck != loto: if loto > 0 and loto < 3: print("closey") luck = random.randint(1, 3) loto = (int(input("Pick a number from 1 to 3: "))) elif loto <= 0 or loto > 3: print("Not closey") luck = random.randint(1, 3) loto = (int(input("NOOOOOPick a number from 1 to 3: "))) else: for i in range(8): print("WIN" * 10)

Oops! While checking some outputs, and reviewing the program, I have noticed there are still some bugs:
Output:
hi 2 Pick a number from 1 to 3: 2 WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN
Output:
hi 1 Pick a number from 1 to 3: 1 WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN
Output:
hi 3 Pick a number from 1 to 3: 3 WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN
Output:
hi 2 Pick a number from 1 to 3: 1 closey Pick a number from 1 to 3: 9 Not closey NOOOOOPick a number from 1 to 3: 3
In your original program:
import random from random import randint print("hi") luck = random.randint(1, 3) print(luck) loto = (int(input("Pick a number from 1 to 3: "))) while "luck" != "loto": if loto > 0 and loto < 3: print("closey") luck = random.randint(1, 3) loto = (int(input("Pick a number from 1 to 3: "))) elif loto <= 0 or loto > 3: print("Not closey") luck = random.randint(1, 3) loto = (int(input("NOOOOOPick a number from 1 to 3: "))) else: for i in range(8): print("WIN" * 10) break
inside the loop, in lines 10 and 14, you have made the program to generate another random number, substituting the original one, which I think it's not the point of the game. Once a number has randomly been generated, at the beginning of the program, you should keep it until it has been correctly guessed. My bad at quickly having a go at changing the program without having checked it properly. In line 8 of your original program, being the values different, the number introduced could have been 1, 2 or 3, not only 1 or 2, as your line 8 implies. So I have made some changes again:
 import random from random import randint print("hi") luck = random.randint(1, 3) print(luck) loto = (int(input("Pick a number from 1 to 3: "))) while luck != loto: if loto > 0 and loto < 4: print("closey") loto = (int(input("Pick a number from 1 to 3: "))) elif loto <= 0 or loto > 3: print("Not closey") loto = (int(input("NOOOOO! Pick a number from 1 to 3: "))) else: for i in range(8): print("WIN" * 10)
I hope not to have left any more bugs with my sleepy head... By the way, I guess you are just practising and seeing what your code does, because otherwise, I wouldn't find very interesting a guessing-a-number game, knowing it from the beginning. I mean, you can store the number generated by the program without printing it on the screen. That is to say, I would delete the line
print(luck)
from the real game. Also, keep in mind that, as the program generates a random number among 1, 2 and 3, it's quite probably that the same number could be generated repeated times (laws of probability)!!! I enclosed some inputs and outputs from running the program included in this answer:
Output:
hi 2 Pick a number from 1 to 3: 2 WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN
Output:
hi 2 Pick a number from 1 to 3: 3 closey Pick a number from 1 to 3: 1 closey Pick a number from 1 to 3: 9 Not closey NOOOOO! Pick a number from 1 to 3: 2 WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN
Output:
hi 1 Pick a number from 1 to 3: 1 WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN
Output:
hi 1 Pick a number from 1 to 3: 0 Not closey NOOOOO! Pick a number from 1 to 3: 1 WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN
Output:
hi 3 Pick a number from 1 to 3: 3 WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN WINWINWINWINWINWINWINWINWINWIN
I wish it helps.

Well explained!! Smash
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Generate only one random number for many tries Bhavika 2 124 Mar-29-2020, 12:12 PM
Last Post: Bhavika
  Finding MINIMUM number in a random list is not working Mona 5 355 Nov-18-2019, 07:27 PM
Last Post: ThomasL
  Inheriting, given number from randint is not working beLIEve 0 202 Oct-12-2019, 06:50 PM
Last Post: beLIEve
  help cant do random number nonzzo 1 476 Mar-28-2019, 07:33 PM
Last Post: Yoriz
  problems with random number/string PrasadPython 3 481 Mar-01-2019, 06:00 PM
Last Post: ichabod801
  randint stops changing values in a loop Naito 4 766 Jan-30-2019, 08:15 AM
Last Post: perfringo
  random number anasa 2 800 Nov-26-2018, 08:56 PM
Last Post: Excelsiscelia
  Random Number sorting Siddharth1989 2 828 Aug-08-2018, 11:53 AM
Last Post: ichabod801
  Help with randint/random Tunechi 1 707 May-14-2018, 02:23 AM
Last Post: micseydel
  Guess Random Number Why i m not able to enter input Nithya Thiyagarajan 6 3,890 Jan-07-2018, 04:26 AM
Last Post: squenson

Forum Jump:


Users browsing this thread: 1 Guest(s)