Python Forum
trying to input a variable using random.choice
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
trying to input a variable using random.choice
#1
I've been messing with mad libs, doing them different ways, shortening code, etc, and i switched from getting user input to letting the program pick the word(s). When letting the user pick the words, i would use list.append to add each word to a list one at a time, then tried using list.extend to add all input variables at one time. When the program picks the word, it displays the word, i have to hit enter to get the next word to display, but after doing list.extend, all the words are empty quotes and that is what i'm having problems with.
import random

list = []

word1 = input(random.choice(open('nouns.txt').read().split()).strip().upper())
word2 = input(random.choice(open('nouns.txt').read().split()).strip().upper())
word3 = input(random.choice(open('nouns.txt').read().split()).strip().upper())
list.extend((word1, word2, word3))
print(list)
what i get is,
Output:
PAN RAINSTORM COUNTRY ['', '', ''] Process finished with exit code 0
Thank you!!!
Reply
#2
here is how the output looks with user input.
import random

list = []

#word1 = input(random.choice(open('nouns.txt').read().split()).strip().upper())
#word2 = input(random.choice(open('nouns.txt').read().split()).strip().upper())
#word3 = input(random.choice(open('nouns.txt').read().split()).strip().upper())
word1 = input('Enter a noun:\n')
word2 = input('Enter a noun:\n')
word3 = input('Enter a noun:\n')
list.extend((word1, word2, word3))
print(list)
Output:
Enter a noun: house Enter a noun: car Enter a noun: train ['house', 'car', 'train'] Process finished with exit code 0
Reply
#3
word1 = input(random.choice(open('nouns.txt').read().split()).strip().upper())
The stuff inside the parenthesis for the input is what the computer prompts you for. So it looks up a random word and prints that while you type in something.

When you just hit return, the empty string is then assigned to word1 (or the other ones). So you're doing all the work of looking up random words, and then throwing them away. The reason you're getting empty strings is because you're just hitting return three times. If you typed something, it would be in the list.
Reply
#4
Using 'list' as a name is not great idea and should be avoided (unless one likes nasty surprises down the road):

>>> spam = list()
>>> spam
[]
>>> list('abc')
['a', 'b', 'c']
>>> list = []
>>> spam = list()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
>>> list('abc')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
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.
Reply
#5
@bowlofred explained what your problem is.

a few additional thoughts

you open and read and close the file for every random word. Open and read the file once, choose n words by using a loop or by taking a random sample (which will guarantee no repetition of words).
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(Aug-13-2020, 05:34 AM)perfringo Wrote: Using 'list' as a name is not great idea and should be avoided (unless one likes nasty surprises down the road):

yeah, i realize that, but just used that in the small demo.
Reply
#7
import random
 
with open('nouns.txt','r') as noun_file:
    nouns = noun_file.read().split().strip().upper()
    words = random.choices(nouns, k=3)

print(words)
Reply
#8
(Aug-13-2020, 03:19 AM)bowlofred Wrote: When you just hit return, the empty string is then assigned to word1 (or the other ones). So you're doing all the work of looking up random words, and then throwing them away. The reason you're getting empty strings is because you're just hitting return three times. If you typed something, it would be in the list.
I realize all that, but i don't understand why it does it and how to remedy it.
Reply
#9
(Aug-13-2020, 02:57 PM)python63 Wrote: I realize all that, but i don't understand why it does it and how to remedy it.

It does it because that's how it's been programmed. Remedy depends on your intent, and I'm not sure I know that.

input() means "ask the user for data". If you want the program to pick the words instead of the user, get rid of the input(). So possibly you want to turn

word1 = input(random.choice(open('nouns.txt').read().split()).strip().upper())
into

word1 = random.choice(open('nouns.txt').read().split()).strip().upper()
That way you keep the data instead of just using it as a prompt to the user.
Reply
#10
perfect! i understand it, and it does just what i want. Thank you!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  combobox_callback(choice choice part of openfile name (kind of dependency) janeik 9 1,432 Sep-10-2023, 10:27 PM
Last Post: janeik
  input variable choice MCL169 7 1,176 Feb-19-2023, 09:00 PM
Last Post: MCL169
  Whys is asterisk and random variable necessary in these functions? rrowhe4d 5 1,508 Aug-05-2022, 07:53 AM
Last Post: Gribouillis
  How to include input as part of variable name Mark17 4 2,496 Oct-01-2021, 06:45 PM
Last Post: Mark17
  random.choice HELP samuelbachorik 4 2,262 Aug-18-2021, 03:24 PM
Last Post: naughtyCat
  Unable to use random.choice(list) in async method spacedog 4 3,418 Apr-29-2021, 04:08 PM
Last Post: spacedog
  Generate Random operator, take user input and validate the user mapypy 4 5,534 Feb-03-2021, 08:41 PM
Last Post: nilamo
  Calling Input for Random Generation ScaledCodingWarrior 1 1,854 Feb-02-2021, 07:27 PM
Last Post: bowlofred
  Help with a random.randint choice in Python booponion 5 2,774 Oct-23-2020, 05:13 PM
Last Post: deanhystad
  ???: if "{choice}" in self._command BaiYouLing4 3 2,017 Aug-23-2020, 05:40 AM
Last Post: BaiYouLing4

Forum Jump:

User Panel Messages

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