Why wont this work?


#1

I’m trying to make a word generator but it wont work when I put in my number.
This is what it said:
Traceback (most recent call last):
File “C:\Users\private\Desktop\private\Programming By Me\Random Word Generator.py”, line 4, in
for number in range (number):
TypeError: ‘str’ object cannot be interpreted as an integer

number = input("How much words would you like to generate?")
import random
a = ["Bird", "Notebook", "Word"]
for i in range (number):
   print (random.choice(a))

What is wrong with it?


#3

Your code works in the codecademy console I’m not sure why its not working offline


#4
number = input("How much words would you like to generate?")
import random
a = ["Bird", "Notebook", "Word"]
for i in range (number):
   print (random.choice(a))

For the sake of order, we generally import all the modules at the start of the program.

import random
number = input("How many words would you like to generate?")
a = ["Bird", "Notebook", "Word"]
for i in range (number):
   print (random.choice(a))

Values returned from input() are type str so they cannot be used in the range() function.

import random
number = input("How many words would you like to generate?")
a = ["Bird", "Notebook", "Word"]
for i in range (int(number)):
   print (random.choice(a))

#5

Thank you!
You helped me a lot.