Python Forum
Working with Random Generated Numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Working with Random Generated Numbers
#1
Hey guys. I have a task that's about working with Random Generated Numbers.
So basically I need to:
Create a program that allows the user to play the lottery. 
● Generate a random two-digit number (between 10 and 99) this number  will be the lottery number. 
● Ask the user to enter any two-digit number this will be the user's guess. 
● Get the first and second digit from the two-digit lottery number. 
● Get the first and second digit from the user's guess. 
● Print out the generated lottery number. 
● If the user's guess matches the lottery number exactly, print out  "Congratulations you have an exact match, you win R10 000.00" (i.e. if the  user enters 12 and the lottery number is 12) 
● If the user's guess matches the lottery numbers, but are in the wrong  order print out "Congratulations you have all digits, you win R5 000.00"  (i.e. if the user enters 48 and the lottery number is 84) 
● If the user guesses one digit correctly print out "Congratulations you have  one correct digit, you win R1 000.00". (i.e. if the user enters 27 and the  lottery number is 78) 
● Else print out "Sorry no match" 

I wrote the code as I think it should go its still work in progress, but my main problem is I cannot seem to separate the random two-digit number from each other and save them as separate variables. I also cant manage to reverse the random-two digit number using an extended slice.

LINE 5
LINE 6
LINE 7

:is where I am struggling to get them separated etc.

I have no problem to separate the users two digit number but I cannot do the same with the randomly generated two-digit number.

Here is my work in progress:

import random

RandomNr = random.randint(10,99)
UserNum = input("Enter any two-digit number: ")
RandomNr1 = RandomNr[0]
RandomNr2 = RandomNr[1]
RandomNr3 = RandomNr[::-1]
UserNum1 = UserNum[0]
UserNum2 = UserNum[1]
print(RandomNr)

if UserNum == RandomNr:
    print("Congratulations you have an exact match, you win R10 000.00")

elif UserNum == RandomNr3:
    print("Congratulations you have all digits, you win R5 000.00")

elif (UserNum1 == RandomNr1)or(UserNum1 == RandomNr2):
    print("Congratulations you have  one correct digit, you win R1 000.00")

elif (UserNum2 == RandomNr1)or(UserNum2 == RandomNr2):
    print("Congratulations you have  one correct digit, you win R1 000.00")

else:
    print("Sorry no match")
      
Thanks in advance for any assistance.

Kind Regards

YoungGrassHopper
Reply
#2
For separating the the the random number, you can simply turn it into a string and get the two numbers. Here's an example put into a function.
def Num_Sep(number):
    strNum = str(number) #Turn the number into a string because strings are made of arrays allowing for the number to be taken apart.
    numberArray = [] #The list to send back the numbers in.
    for character in strNum:
        numberArray.append(int(character)) #Add the number to the list as an int value.
    
    return numberArray #Return the numbers of the number in a list.
Hope this helps!
Reply
#3
(Sep-09-2019, 08:54 PM)SheeppOSU Wrote: For separating the the the random number, you can simply turn it into a string and get the two numbers. Here's an example put into a function.
def Num_Sep(number):
    strNum = str(number) #Turn the number into a string because strings are made of arrays allowing for the number to be taken apart.
    numberArray = [] #The list to send back the numbers in.
    for character in strNum:
        numberArray.append(int(character)) #Add the number to the list as an int value.
    
    return numberArray #Return the numbers of the number in a list.
Hope this helps!


Thanks SheepOSU for the help, I am going to try and implement it, I am still very new to coding, like I started past Thursday with a software engineering bootcamp from scratch ive never touched coding before so its all very new and foreign to me and takes me a while to digest it but I am sure I will figure it out with your advice I appreciate it big time SheepOSU! :)
Reply
#4
The reason you cannot separate the two digits is because it is of type int. It must be a str() to be able to extract one digit at a time.

John
Reply
#5
(Sep-09-2019, 08:54 PM)SheeppOSU Wrote: For separating the the the random number, you can simply turn it into a string and get the two numbers. Here's an example put into a function.
def Num_Sep(number):
    strNum = str(number) #Turn the number into a string because strings are made of arrays allowing for the number to be taken apart.
    numberArray = [] #The list to send back the numbers in.
    for character in strNum:
        numberArray.append(int(character)) #Add the number to the list as an int value.
    
    return numberArray #Return the numbers of the number in a list.
Hope this helps!

Thanks SheeppOSU my code runs now, I just casted RandomNr1 ; RandomNr2 and RandomNr3 to str and I fixed the elif statements , the "or" seemed to spawn a issue giving a false positive in some cases so I just put them in separate elif statements instead of "or"

#This program allows the user to play a high odds two-digit lottery.
import random

RandomNr = random.randint(10,99)#Generating random two-digit number.
UserNum = input("Enter any two-digit number: ") #Getting users two-digit number.
RandomNr1 = str(RandomNr)[0] #Seperating random number into two, single digits.
RandomNr2 = str(RandomNr)[1] #Seperating random number into two, single digits.
RandomNr3 = str(RandomNr)[::-1]#Reversing random two-digit number.
Thanks for the help on this !!

(Sep-09-2019, 10:38 PM)jsira2003 Wrote: The reason you cannot separate the two digits is because it is of type int. It must be a str() to be able to extract one digit at a time. John

Thanks John I appreciate your reply very much, I changed the code and it runs now. ;) I am a terrible newbie haha, I am amazed at the quick and prompt help I received on this forum so far, spectacular stuff!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  random numbers, randint janeik 2 561 Nov-27-2023, 05:17 PM
Last Post: janeik
  HOW TO USE C# GENERATED DLL davide_vergnani 2 1,632 Jun-12-2023, 03:35 PM
Last Post: davide_vergnani
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,198 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List of random numbers astral_travel 17 2,693 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,508 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  random.uniform is not working correctly dron4ik86 9 4,107 Feb-06-2020, 03:04 PM
Last Post: dron4ik86
  output a list of random numbers 'x' columns wide adityavpratap 4 2,970 Jan-13-2020, 05:32 PM
Last Post: perfringo
  Can i prevent the random generator to generate already used numbers? MauserMan 3 2,859 Jan-05-2020, 04:44 PM
Last Post: MauserMan
  Finding MINIMUM number in a random list is not working Mona 5 3,041 Nov-18-2019, 07:27 PM
Last Post: ThomasL
  random selection of song and artist not working Unknown_Relic 2 2,344 Nov-08-2019, 02:06 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020