Python Forum
trying to input a variable using random.choice - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: trying to input a variable using random.choice (/thread-28990.html)



trying to input a variable using random.choice - python63 - Aug-13-2020

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!!!


RE: trying to input a variable using random.choice - python63 - Aug-13-2020

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



RE: trying to input a variable using random.choice - bowlofred - Aug-13-2020

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.


RE: trying to input a variable using random.choice - perfringo - Aug-13-2020

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



RE: trying to input a variable using random.choice - buran - Aug-13-2020

@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).


RE: trying to input a variable using random.choice - python63 - Aug-13-2020

(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.


RE: trying to input a variable using random.choice - deanhystad - Aug-13-2020

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)



RE: trying to input a variable using random.choice - python63 - Aug-13-2020

(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.


RE: trying to input a variable using random.choice - bowlofred - Aug-13-2020

(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.


RE: trying to input a variable using random.choice - python63 - Aug-13-2020

perfect! i understand it, and it does just what i want. Thank you!!