Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random Choice Operations
#1
The problem here is i successfully pick a random number from list but when i want o divide that number to 2 just like last lane of my code python chooses other random number and divide that number to 2
Thanks in advance for answers.


forum. ie.
import random
numberList = [2,4,6,8,10]
random_num = random.choice(numberList)
print(random.choice(numberList))
print("result is : " + str(random_num))/2 
Reply
#2
Your code is trying to divide the print statement by 2
Error:
TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'
Using f string formating
print(f'result is : {random_num/2}')
Reply
#3
still python chooses new number and divide the new number , in my code i successfully divide the random number to 2 but the problem it chooses new number i need the first chosen random number look i changed the code as u said
forum. ie.
numberList = [2,4,6,8,10]
random_num = random.choice(numberList)
print("additional: ", + random.choice(numberList))

input("division 2")
input("Subtract the first number you have chosen")
print("Hmmm  let me think")
time.sleep(2)
print("Result issss")
print(f'result is : {random_num/2}')
result
additional: 10
division 2
Subtract the first number you have chosen
Hmmm let me think
Result issss
result is : 1.0
last result is 1.0 which should be 5 because additional is first random choice number which is 10 so our code sohuld give us 5 but it didnt
Reply
#4
I don't understand what you are trying to do.
You will have two different random numbers as you have used random.choice(numberList) twice.

Could you explain in non python code what you are trying to achieve.
Reply
#5
i want to make python chose a random number after i want to print some text than i want to print that chosen number, for example :

random.choice = 5
hello world
tnx yoriz
print the random choice number (here i want python to print me the 5 again )

but each time i try to do that python choose new number i just want to print the First chosen number again
Reply
#6
Obtain the random choice once only

import random

numberList = [2, 4, 6, 8, 10]
random_num = random.choice(numberList)

print(f'random.choice = {random_num}')
print('hello world')
print('tnx yoriz')
print(('the random choice number '
       f'(here i want python to print me the {random_num} again)'))
Output:
random.choice = 4 hello world tnx yoriz the random choice number (here i want python to print me the 4 again)
Reply
#7
tnx alot finaly i got it :)

here what i made it for you to see , i appreciate a lot your help /try the code u may surprise :)
forum. ie.
import random


import time


num =(input("Choose a number between 1-10 must be even and remember it"
            " (When you ready press ENTER)"))
input("times 2")

numberList = [2,4,6,8,10]
random_num = random.choice(numberList)
input(f'additional {random_num}')

input("division 2")
input("Subtract the first number you have chosen")
print("Hmmm  let me think")
time.sleep(2)
print("Result issss")
print(f'result is : {random_num/2}')

exit()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  combobox_callback(choice choice part of openfile name (kind of dependency) janeik 9 1,448 Sep-10-2023, 10:27 PM
Last Post: janeik
  Replicate Excel operations with Python Lumberjack 3 1,824 May-10-2022, 01:44 AM
Last Post: Lumberjack
  random.choice HELP samuelbachorik 4 2,277 Aug-18-2021, 03:24 PM
Last Post: naughtyCat
  Program demonstrates operations of bitwise operators without using bitwise operations ShawnYang 2 1,791 Aug-18-2021, 03:06 PM
Last Post: deanhystad
  Unable to use random.choice(list) in async method spacedog 4 3,440 Apr-29-2021, 04:08 PM
Last Post: spacedog
  Help with a random.randint choice in Python booponion 5 2,789 Oct-23-2020, 05:13 PM
Last Post: deanhystad
  ???: if "{choice}" in self._command BaiYouLing4 3 2,035 Aug-23-2020, 05:40 AM
Last Post: BaiYouLing4
  trying to input a variable using random.choice python63 9 3,631 Aug-13-2020, 05:37 PM
Last Post: python63
  Create bot to automate operations in IQ Option JonatasCavalini 0 12,533 Jul-19-2020, 02:23 AM
Last Post: JonatasCavalini
  Two operations in two ranges salwa17 3 2,143 Jun-22-2020, 04:15 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