Posts: 11
Threads: 8
Joined: Jun 2019
Jul-16-2019, 09:45 AM
(This post was last modified: Jul-16-2019, 09:46 AM by Gateux.)
Hi I am trying to generate a random sequence of numbers base on user input but right now I only know of this code:
import random
def main():
list = [1, 2, 3, 4, 5]
test = random.choice(list)
print(test) Would like the result to store in a list, how can I do that base on input? thanks.
Posts: 1,950
Threads: 8
Joined: Jun 2018
(Jul-16-2019, 09:45 AM)Gateux Wrote: Hi I am trying to generate a random sequence of numbers base on user input
I don't see any user input. Where and what should user enter?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 11
Threads: 8
Joined: Jun 2019
I'm trying to develop a program where the user can enter any integer e.g 7 and then a random sequence of whole numbers from 1 to that number is then generated, e.g., 5 2 4 1 6 7 3.
e.g number = int(input("Enter the max number to be generated from 1: "))
then the next line will display "Current number is 1" and then prompt the user to enter a guess to see if the number if higher or lower than 1.
Posts: 1,950
Threads: 8
Joined: Jun 2018
(Jul-16-2019, 10:55 AM)Gateux Wrote: I'm trying to develop a program where the user can enter any integer e.g 7 and then a random sequence of whole numbers from 1 to that number is then generated, e.g., 5 2 4 1 6 7 3.
e.g number = int(input("Enter the max number to be generated from 1: "))
then the next line will display "Current number is 1" and then prompt the user to enter a guess to see if the number if higher or lower than 1.
I can follow this:
- ask from user a number and convert to int
- create random ordered list from range(1, user_number + 1)
But I am totally lost here: "display "Current number is 1" and then prompt the user to enter a guess to see if the number if higher or lower than 1"
If current number is 1 what number is higher or lower than 1?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 11
Threads: 8
Joined: Jun 2019
def main():
n = int(input("Enter number: "))
for i in range(1, n + 1):
myList = [i]
print(myList, end =" ") If I enter the int 5, I get the output as [1] [2] [3] [4] [5] but I want the numbers to be in random orders instead.
Example the input of the int is 7 so the random order should be something like this or any random order [5] [2] [4] [1] [6] [7] [3]
After the user input the int, the list containing the random orders number are stored in a variable not visible to the user. The first number will then be revealed to the user and he must guess if the next random number is higher or lower than this current first number shown.
Posts: 1,950
Threads: 8
Joined: Jun 2018
What would you do if you want to have random order? Shuffle.
random.shuffle() shuffles list in place and returns None:
>>> lst = list(range(1, 11))
>>> random.shuffle(lst)
>>> lst
[6, 5, 2, 1, 7, 9, 8, 10, 4, 3]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 11
Threads: 8
Joined: Jun 2019
import random
def main():
n = int(input("Enter number: "))
random.shuffle(n)
for i in range(n):
print(i) I get error:
Error: for i in reversed(range(1, len(x))):
TypeError: object of type 'int' has no len()
I understand from the error it's telling me len() function cannot be used for integer data type. Can you guide me how can I include the random.shuffle in my code above?
Posts: 4
Threads: 2
Joined: Jul 2019
Jul-16-2019, 03:42 PM
(This post was last modified: Jul-16-2019, 03:44 PM by JBristow1729.)
import random
def main():
m = int(input("Enter max number: "))
lst = list(range(1, m))
random.shuffle(lst)
for i in range(1, m+1):
currentnum = lst[i]
print(currentnum) This is what you're asking for... although I'm not sure what it's trying to achieve.
Posts: 22
Threads: 12
Joined: Jul 2019
Please try this code. This will generate random number and put them in a list but each items in the list is unique.
import random
list = [] # An empty list
i = int(input("Enter the number: "))
# print(random.randint(1, i+1))
for j in range(0, i):
k = random.randint(1, i)
while True:
if k in list:
k = random.randint(1, i)
else:
break
list.append(k)
print(list)
|