Maze Game - input expected at most 1 arguments, got 3 - python

import random
from random import randint
print("Welcome to Brandon's Maze Game",
"You have to get 10 shields or find the exit to win",
"Good Luck :D")
counter = 0
shields = 3
fate = randint(1,2)
direction = input("You have come to a stop, do you want to turn Left(L) or Right(R)? ")
if direction == "L":
if fate == 1:
shields += 3
counter += 1
direction = input("You came across a chest, you now have ",shields, "! What way do you want to go next? Left(L) or Right(R)? ")
if fate == 2:
shields -= 1
counter += 1
direction = input("Uh oh, you got attacked and lost a shield, you now have ",shields," shields. Do you want to go Left(L) or Right(R)? ")
if direction == "R":
if fate == 1:
shields += 3
counter += 1
direction = input("You came across a chest, you now have ",shields, "! What way do you want to go next? Left(L) or Right(R)? ")
if fate == 2:
shields -= 1
counter += 1
direction = input("Uh oh, you got attacked and lost a shield, you now have ",shields," shields. Do you want to go Left(L) or Right(R)? ")
if counter == 10:
print("Congratulations, you made it to the end with ",shields," shields.")
I am making a maze game where the user has the option to go Left("L") or Right("R"), then the program makes a choice whether to make the player find a chest or be attacked. When the user finds a chat they get +3 shields, if they are attacked they lose 1 shield.
When I enter "L" or "R" it says: on line 19
TypeError: input expected at most 1 arguments, got 3.
Not sure what's going on as I am only inputting 1 value?,
Any help is appreciated.

You are giving input three arguments (string, int, string). Here's a demo with a function that prints its number of arguments:
>>> shields = 42
>>> def foo(*args):
... print(len(args))
...
>>> foo('fiz', shields, 'buzz')
3
What you want is to give one string to input:
>>> foo('fiz {} buzz'.format(shields))
1

input() is not like print(). The prompt is a single string, so concatenate strings with + (where you have comma). Note that you have to convert non-strings (like int) into a string.
print("Congratulations, you made it to the end with " + str(shields) + " shields.")
or string formatting:
print("Congratulations, you made it to the end with {:d} shields.".format(shields))
or use literal string interpolation if you are on Python 3.6
print(f"Congratulations, you made it to the end with {shields} shields.")

So the problem is that you had more than on parameter in the input function which unlike print you can not do. This can be solved easily using a plus sign to concatenate the strings together instead of a comma, but to do this you have to convert the int shield to a string by just putting str(shield) to convert it. this is what I have written python 2.7 so you might have to change a few things but it should all be there. I also added .upper() to the if statements so it can take both upper and lower case inputs. ps for python 3 you will do input() not raw_input(). sorry for writing in 2.7 i am not the good with 3. If you have any question feel free to ask
import random
from random import randint
print("Welcome to Brandon's Maze Game",
"You have to get 10 shields or find the exit to win",
"Good Luck :D")
counter = 0
shields = 3
fate = randint(1,2)
direction = raw_input("You have come to a stop, do you want to turn Left(L) or Right(R)? ")
if direction.upper() == "L":
if fate == 1:
shields += 3
counter += 1
direction = raw_input("You came across a chest, you now have "+str(shields)+ "! What way do you want to go next? Left(L) or Right(R)? ")
if fate == 2:
shields -= 1
counter += 1
direction = raw_input("Uh oh, you got attacked and lost a shield, you now have "+str(shields)+" shields. Do you want to go Left(L) or Right(R)? ")
if direction.upper() == "R":
if fate == 1:
shields += 3
counter += 1
direction = raw_input("You came across a chest, you now have "+str(shields)+"! What way do you want to go next? Left(L) or Right(R)? ")
if fate == 2:
shields -= 1
counter += 1
direction = raw_input("Uh oh, you got attacked and lost a shield, you now have "+str(shields)+" shields. Do you want to go Left(L) or Right(R)? ")
if counter == 10:
print("Congratulations, you made it to the end with "+str(shields)+" shields.")

Related

Players are unable to win in my python game, keeps saying value is higher

I have a guessing game where one player sets the number for the other person to guess, and then the other player tries to guess what the number is, and the game says if the number is higher, or lower than their current guess. My code is pretty messy, and overcomplicated since I am new to Python. But it just recently broke, not letting the player wins. When the player should win, it says that the number is higher. Here's my code:
from random import randint
import getpass
# Allows the user to select the range
guessamount = input("How large do you want the range of numbers to pick be?(Limit is 420): ")
if int(guessamount) >= int(420):
guessamount = 420
# Allows player 1 to select the answer for player 2 to guess
answer = getpass.getpass("Player 1 select the answer (The answer will not show up while typing):")
if int(answer) >= int(guessamount):
answer = guessamount
attempts = 5
while attempts != 0:
guess = input("Guess a number between 1-" + str(guessamount) + ": ")
if answer == int(guess):
attempts = 0
else:
attempts -= 1
if int(guess) > int(answer):
print ("You suck at this game. The answer is lower than " + str(guess))
else:
print ("You suck at this game. The answer is higher than " + str(guess))
if answer == int(guess):
print("You won the game! The answer was " + str(answer))
else:
print("You ran out of attempts. The number was " + str(answer))
I fixed it! I changed it so the player inserts the answer instead of it being random. And I forgot to change the numbers to int.
Thanks #jarmod for helping.

How can I stop the loop after max wrong answer?

This is an exercise from Introduction to CompSci and Programming in Python from OCW MIT. I modified it a little bit. The problem is I want to stop the program when I reached the max wrong answer but it stops if I try two times. Why two times ? How can I fix this ? As a new starter should I ask every question here ? Thanks to all
n = 0
max_guesses = 3
n = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")
if n == "left" or "Left":
print("You're out of lost forest")
while n == "right" or n == "Right":
n = input("You are in the Lost Forest\n****************\n****** ***\n :(\n****************\n****************\nGo left or right? ")
n =+ 1
for n in range (max_guesses):
break
print("Game over! You ran out of your lives")
There are a few errors that contribute to your code not working properly
Your variable n is the same as the value you get from the value you input best to separate those into different values to make it easier to avoid hard to detect errors
You need to have a condition that checks if n >= max_guesses.
Welcome to the community, we try to discuss the issues here rather than looking for a whole solution.
I suggest you to first learn the very basics of programming like the decision making, control flows (i.e if statements and loops).
Also, try to attempt the question, multiple numbers of times with different logics and inputs at runtime. it will give you a better understanding of logical analysis.
Here's one of the ways you could have added the logic.
#Maximum allowed guesses
max_guesses = 3
def func1():
#Input string 'left' or 'right'
in_str = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")
#Number of attempts made
n = 1
if in_str.lower() == "left":
print("You're out of lost forest")
while in_str.lower() == "right":
in_str = input("You are in the Lost Forest\n****************\n****** ***\n :(\n****************\n****************\nGo left or right? ")
n += 1
if in_str.lower() == "left":
print("You're out of lost forest")
break
if n >= max_guesses:
print("Game over!!!")
break
print("Total attempts made by user: ",n)
func1()
I have attempted to fix your code however, I have had to remove the for loop and change the variable names. Hopefully, it helps.
n = 1
max_guesses = 3
a = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")
if a.lower() == "left":
print("You're out of lost forest")
while a.lower() == "right":
if n == 3:
print("Game over! You ran out of your lives")
break
else:
a = input("You are in the Lost Forest\n****************\n****** ***\n :(\n****************\n****************\nGo left or right? ")
n += 1

What can I do to fix my simple computer-player game code? Syntax Error

I'm new to Python and I wanted to practice doing loops because I’ve been having the most trouble with them. I decided to make a game where the user will pick a number from 0-100 to see if they can win against the computer.
What I have going right now is only the beginning. The code isn’t finished. But trying out the code I got a Syntax error where the arrow pointed at the colon on the elif function.
How do I fix this? What can I do?
I accept any other additional comments on my code to make it better.
Here’s my code:
import random
min = 0
max = 100
roll_again = "yes"
quit = "no"
players_choice = input()
computer = random.randint
while roll_again == "yes":
print("Pick a number between 1-100: ")
print(players_choice)
if players_choice >= 0:
print("Your number of choice was: ")
print(players_choice)
print("Your number is high.")
if computer >= 0:
print("Computers number is: ")
print(computer)
print("Computers number is high.")
if computer >= players_choice:
print("Computer wins.")
print("You lose.")
print("Would you like to play again? ", +roll_again)
elif:
print(quit)
end
Goal:
Fix computer-player game while learning more about python. Providing additional documentation on where to start would be helpful.
The reason you are getting an error pointing to elif is because elif needs a condition to check. You need to use if elif and else like this:
if a == b:
print('A equals B!')
elif a == c:
print('A equals C!')
else:
print('A equals nothing...')
Also, Python relies on indentation to determine what belongs to what, so make sure you are paying attention to your indents (there is no end).
Your code has more errors after you fix the if statements and indentation, but you should be able to look up help to fix those.
There are a lot of problems with your code. Here is a working version, hope it helps you understand some of the concepts.
If not, feel free to ask
import random
# min and max are names for functions in python. It is better to avoid using
# them for variables
min_value = 0
max_value = 100
# This will loop forever uless something 'breaks' the loop
while True:
# input will print the question, wait for an anwer and put it in the
# 'player' variable (as a string, not a number)
player = input("Pick a number between 1-100: ")
# convert input to a number so we can compare it to the computer's value
player = int(player)
# randint is a function (it needs parentheses to work)
computer = random.randint(min_value, max_value)
# print what player and computer chose
print("Your choice: ", player)
print("Computer's choice: ", computer)
# display the results
if computer >= player:
print("Computer wins. You loose")
else:
print("you win.")
# Determine if user wants to continue playing
choice = raw_input("Would you like to play again? (yes/No) ")
if choice != 'yes':
# if not
break
There are a lot of indentiation issues and the if and elif statements are used incorrectly. Also take a look at how while loops work.
Based on the code you provided here is a working solution, but there are many other ways to implement this.
Here is some helpful tutorials for you on if/else statements as well as other beginner topics:
Python IF...ELIF...ELSE Statements
import random
minval = 0
maxval = 100
roll_again = "yes"
quit_string = "no"
while True:
players_choice = int(input("Pick a number between 1-100:\n"))
computer = random.randint(minval,maxval)
#checks if players choice is between 0 and 100
if players_choice >= 0 and players_choice <= 100:
print("Your number of choice was: ")
print(players_choice)
#otherwise it is out of range
else:
print("Number out of range")
#check if computers random number is in range from 0 to 100
if computer >= 0 and computer <= 100:
print("Computers number is: ")
print(computer)
# if computer's number is greater than players number, computer wins
if computer > players_choice:
print("Computer wins.")
print("You lose.")
#otherwise if players number is higher than computers, you win
elif computer < players_choice:
print("You won.")
#if neither condition is true, it is a tie game
else:
print("Tied game")
#ask user if they want to continue
play_choice = input("Would you like to play again? Type yes or no\n")
#checks text for yes or no use lower method to make sure if they type uppercase it matches string from roll_again or quit_string
if play_choice.lower() == roll_again:
#restarts loop
continue
elif play_choice.lower() == quit_string:
#breaks out of loop-game over
break

Im unable to stop the input from taking place & play again loop middle of script

I am making a custom version of hangman for a college assignment using python, but currently I'm having an issue with two parts to my code, its a game of hangman.
I have included the entire code below so that if anyone can answer they have the full information from the code.
The issues I'm having are that this part of the code is not ignoring the input before so it still lets you guess multiple letters.
if len(guess) > 1:
print("Ye can't be cheatin' now, ye be using up ya guesses.")
failed += 1
The second issue I have is that once you win the game it doesn't ask if you want to play again properly (I have tried multiple methods to try to fix this including another while loop inside the if statement.
if failed == 0:
print("Ye be living for now")
print("The word was: " + myword)
print
...
while True:
...
if failed == 0:
print("Ye be living for now")
print("The word was: " + myword)
# Current Bug: Play again here, cannot use same line as end.
print
guess = input("guess a letter: ").lower()
if len(guess) > 1:
print("Ye can't be cheatin' now, ye be using up ya guesses.")
failed += 1
# Current Bug: still takes input into consideration when it shouldn't
guesses += guess
...
play_again = input("If you'd like to play again, please type 'yes' or 'y': ").lower()
if play_again == "yes" or play_again == "y":
continue
else:
play_again != "yes" or play_again != "y"
break
This should solve your problem of taking multiple letters as input
if len(guess) > 1:
print("Ye can't be cheatin' now, ye be using up ya guesses.")
failed += 1
# Current Bug: still takes input into consideration when it shouldn't
else:
guesses += guess
This will only add the input to guesses if its length is less than 1.
And if you want to ask the game to play again, after winning put your code in a while loop. Something like this:
play_again = 'y'
while(play_again == 'y' or play_again='yes'):
win = False
while(!win):
<your code for game, set win = True after winning>
play_again = input("Want to play again y or n: ").lower()
Edit: You can add failed along with win in the while loop as well.

How to avoid repeating strings?

My code is:
import random
WORDS = ('python', 'football', 'facebook', 'photo') #list of words that will be riddled
word = random.choice(WORDS)
correct = word
jumble = ''
hint = 'hint'
score = 0
while word:
position = random.randrange(len(word))
jumble += word[position]
word = word[:position] + word[(position + 1):] #creating jumble of correct words
print('Welcome to the game "Anagrams"')
print('Here`s your anagram:', jumble) #Welcoming and giving a jumble to a player
guess = input('\nTry to guess the original word: ')
if guess == correct:
score += 5
print('You won! Congratulations!') #end of game in case of right answer
if guess == hint: #situation if player asks a hint
if correct == WORDS[0]:
print('snake')
elif correct == WORDS[1]:
print('sport game')
elif correct == WORDS[2]:
print('social network')
elif correct == WORDS[3]:
print('picture of something')
score += 1
while guess != correct and guess != '': #situation if player is not correct
print('Sorry, you`re wrong :(')
guess = input('Try to guess the original word: ')
print('Thank you for participating in game.')
print('Your score is', score)
input('\nPress Enter to end')
When asking hint string :
'Sorry, you`re wrong :('
repeats.
It looks like:
Try to guess the original word: hint
sport game
Sorry, you`re wrong :(
How to make this string appear only in case of wrong guess?
change you last while to this:
while guess != correct and guess != '':
guess = input("Sorry, you`re wrong:( ")
In your code, when the player types hint the player gets a hint, but then the program tests the 'hint' string against the correct word. Of course, 'hint' isn't the correct answer, so your program tells them that it's wrong.
Just for fun, I've optimized your code a little, and improved the scoring logic. :)
Your letter-jumbling for loop is quite clever, but there's a more efficient way to do this, using the random.shuffle function. This function shuffles a list, in place. So we need to convert the chosen word into a list, shuffle it, and then join the list back into a string.
I've also replaced your hints logic. Rather than having to do a whole bunch of if tests to see which hint goes with the current word it's much simpler just to store each word and its associated hint as a tuple.
import random
#Words that will be riddled, and their hints
all_words = (
('python', 'snake'),
('football', 'sport game'),
('facebook', 'social network'),
('photo', 'picture of something'),
)
#Randomly choose a word
word, hint = random.choice(all_words)
#Jumble up the letters of word
jumble = list(word)
random.shuffle(jumble)
jumble = ''.join(jumble)
print('Welcome to the game "Anagrams"\n')
print('You may ask for a hint by typing hint at the prompt')
print('Wrong guesses cost 2 points, hints cost 1 point\n')
print("Here's your anagram:", jumble)
score = 0
while True:
guess = input('\nTry to guess the original word: ')
if guess == word:
score += 5
print('You won! Congratulations!')
break
if guess == 'hint':
#Deduct a point for asking for a hint
score -= 1
print(hint)
continue
#Deduct 2 points for a wrong word
score -= 2
print('Sorry, you`re wrong :(')
print('Thank you for participating in game.')
print('Your score is', score)
input('\nPress Enter to end')
Your special logic for a correct guess and for the special input "hint" is only run once on the very first guess. Your loop for incorrect values always runs after that. I think you want to move all the logic into the loop:
while True: # loop forever until a break statement is reached
guess = input('\nTry to guess the original word: ')
if guess == correct:
score += 5
print('You won! Congratulations!')
break # stop looping
if guess == hint: # special case, asking for a hint
if correct == WORDS[0]:
print('snake')
elif correct == WORDS[1]:
print('sport game')
elif correct == WORDS[2]:
print('social network')
elif correct == WORDS[3]:
print('picture of something')
score += 1
else: #situation if player is not correct, and not askng for a hint
print('Sorry, you`re wrong :(')
I've left out the situation where your code would exit the loop on an empty input. If you want that, you should add it explicitly as an extra case, with a break statement.
Lets try to fix some problems:
this
if guess == hint: #situation if player asks a hint
should probably be
elif guess == hint: #situation if player asks a hint
And also this seems wrong to me
while guess != correct and guess != '': #situation if player is not correct
print('Sorry, you`re wrong :(')
guess = input('Try to guess the original word: ')
should be probably changed into that (indentation is important):
guess = input('Try to guess the original word: ')
if guess != correct and guess != '': #situation if player is not correct
print('Sorry, you`re wrong :(')
I have not tried this corrections in a complete program.

Categories

Resources