In: Computer Science
I wrote this code and it produces a typeError, so please can you fix it?
import random
def first_to_a_word():
print("###### First to a Word ######")
print("Instructions:")
print("You will take turns choosing letters one at a time until a
word is formed.")
print("After each letter is chosen you will have a chance to
confirm whether or not a word has been formed.")
print("When a word is formed, the player who played the last letter
wins!")
print("One of you has been chosen at random to initiate the
game.")
print()
print("Note: Words must be longer than a single letter!")
print()
#inputs / randomNumGen
player1 = input("Enter player 1's name: ")
player2 = input("Enter player 2's name: ")
random_number = random.randint(0, 1)
#output function
def output_func(player1, player2, random_number):
if random_number == 0:
word = input(player1, 'please enter a character: ')
print(player1, 'input:', word)
else:
word = input(player2, 'please enter a character: ')
print(player2, 'input:', word)
return word
#initial variables
word_confirm = ''
counter = 0
word_array = []
#character input loop
while word_confirm != 'yes':
#function call
word = output_func(player1, player2, random_number)
#append
word_array.append(word)
#alternate players turns
if random_number == 0:
random_number = 1
else:
random_number = 0
#check for exit after >=3 letters formed
if counter >= 2:
print('You have formed the letters:') #three letters
word_confirm = input('Has the word been formed? (type "yes" or
"no"): ')
word_confirm = word_confirm.lower()
counter += 1
complete_word = ''
i = 0
while i < len(word_array):
complete_word += word_array[i]
i += 1
if random_number == 1:
print(player1, 'Wins!')
print('The word was:', complete_word)
else:
print(player2, 'Wins!')
print('The word was:', complete_word)
first_to_a_word()
Hi,
Hope you are doing fine. You have a super clean job with the code. However, there was only one error in output_func() which has been corrected. Also I have slighly modifies the sequence of program as according to the original code, the instructions are printed after finishing the game. I have made sure that there are printed before taking the player names as input. Coming to the error, after the compilation of the given code, you'll see an error something like this:
Explanation:
The error is because the predefined function input() considers player1 as an additional parameter. Unlike print() which will append the name of player prior to the statement in quotes, input() will consider this as a parameter and will raise a TypeError. In order to fix this replace ',' with a concatenation operator '+'. This will clearly indicate input() that value at player1 must be concatenated to the next statement.
Fix:
Corrected code:
import random
def first_to_a_word():
print("###### First to a Word ######")
print("Instructions:")
print("You will take turns choosing letters one at a time until a
word is formed.")
print("After each letter is chosen you will have a chance to
confirm whether or not a word has been formed.")
print("When a word is formed, the player who played the last letter
wins!")
print("One of you has been chosen at random to initiate the
game.")
print()
print("Note: Words must be longer than a single letter!")
print()
#printing game instructions
first_to_a_word()
print()
#inputs / randomNumGen
player1 = input("Enter player 1's name: ")
player2 = input("Enter player 2's name: ")
random_number = random.randint(0, 1)
#output function
def output_func(player1, player2, random_number):
if random_number == 0:
word = input(player1+ ' please enter a character: ')
print(player1, 'input:', word)
else:
word = input(player2+ ' please enter a character: ')
print(player2, 'input:', word)
return word
#initial variables
word_confirm = ''
counter = 0
word_array = []
#character input loop
while word_confirm != 'yes':
#function call
word = output_func(player1, player2, random_number)
#append
word_array.append(word)
#alternate players turns
if random_number == 0:
random_number = 1
else:
random_number = 0
#check for exit after >=3 letters formed
if counter >= 2:
print('You have formed the letters:') #three letters
word_confirm = input('Has the word been formed? (type "yes" or
"no"): ')
word_confirm = word_confirm.lower()
counter += 1
complete_word = ''
i = 0
while i < len(word_array):
complete_word += word_array[i]
i += 1
if random_number == 1:
print(player1, 'Wins!')
print('The word was:', complete_word)
else:
print(player2, 'Wins!')
print('The word was:', complete_word)
Executable code snippet from jupyter notebook:
Output: