I'm new to python, I started learning a couple days ago. I'm learning from this youtube video on python from "freecodecamp" (https://www.youtube.com/watch?v=rfscVS0vtbw&t=6831s). In the video he was teaching the audience how to make a guessing game. In the game, the user gets 3 chances to guess the secret word. I'm on the latest python version, and I wanted to know, how I can make it so that after every incorrect guess, it tells the user how many guesses they have left. Here is the code so far:
print("You need to guess a yellow fruit!")
secret_word = "Bananas"
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False
while guess != secret_word and not(out_of_guesses):
if guess_count < guess_limit:
guess = input("Enter guess: ")
guess_count += 1
else:
out_of_guesses = True
if out_of_guesses:
print("Out of guesses, you lose!")
else:
print("You win!")
Any help would be appreciated, Thank you!
The easiest way to do that is to use an f string. F strings make it very easy to put variables into a string, and they are awesome. You would use something like print(f"you have {guess_limit-guess_count} tries left!!") on each iteration.
You can also do something like this with a placeholder
print("You have {0} trys left!!").format(guess_limit-guess_count)
It will replace the {0} with whatever you put into the format
You can also read about different ways of doing this from here
Related
I am just starting out in learning python and I was trying to create a word guessing game where I have a secret word and the user has 3 attempts to guess it.
I basically finished the game but am stuck on one part of it.
Whenever I run my code, and guess the word in the first try, the terminal prints two successful print statements. For example, on line 12, I code that if the user guesses the word then print "Good Job, You win!"but whenever I guess the word in first try it also prints "You have just won" on line 24 (I coded this line so if the user guesses the word after 1st try it should print this).
SO, is there a way where I can end the code after line 12 if the condition is met?? so it does not print both messages on line 12 and 24 if guessed right in the first try.
please help this beginner noob out. Thanks!
secret_word = "Tiger"
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False
print("Hello, welcome to the guessing game!")
x = input("Please guess the secret word: ")
if x == secret_word:
print("Good Job, You win!")
#end the code here and not run anything after if user guesses the right word
while guess != secret_word and x != secret_word and not(out_of_guesses):
if guess_count < guess_limit:
guess = input("Wrong, enter your guess word again: ")
guess_count = guess_count + 1
else:
out_of_guesses = True
if out_of_guesses:
print("Sorry, You have run out of guesses.")
else:
print("You have just won")
You could call the built-in quit() function. If/when you modularize your code into functions, this could be done with a return call.
The quit() function will basically tell the program to immediately end and will not execute the rest of the code.
You can import sys and use sys.exit()
Some other ways
I made a Guessing game in python in which there will be a String That you will have to guess and input it. You get 3 tries and if failed a printed statement will show up saying "Game lost", But I am getting errors that I am unable to resolve. Here is the code:
Total_Guesses = 3
Guess = ""
G = 1
Seceret_Word = "Lofi"
Out_of_Guesses = False
while guess != Seceret_Word and not(Out_of_Guesses):
if G < Number_of_Guesses
guess = input("Enter your guess:")
G += 1
else:
Out_of_Guesses = True
if Out_of_Guesses:
print("OUT OF GUESSES")
else:
print("you win")
Please share your error(s) with us.
First of all there are simple syntax problems in your code. Indentation is very important concept in Python.
Indentation:
while something:
doThis()
or something like this,
if something:
doThat():
Also there are some variables you didn't define. If you don't define you can't use them. guess, Number_of_Guesses These are very important things if you are beginner.
Here is a fixed version of your code:
total_guess_count = 3
guess_count = 0
secret_word = "Lofi"
while guess_count < total_guess_count:
guess = input("Enter your guess: ")
if guess == secret_word:
print("You win!")
break
else:
guess_count += 1
if guess_count == total_guess_count:
print("You lost!")
else:
print("Try again!")
You should obey the naming variable rules to get better. Such as, generally you don't start with uppercase characther. You defined "Guess" but tried to check for "guess" in while condition.
And you should start to check winning condition first, if you try to make it in else body, program wouldn't work healthy. I mean yes it can work but probably it would be buggy.
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 1 year ago.
def guessNum():
guess = input("guess a number between 1 and 10: ")
guesses = 3
randomNum = 7
while guess != randomNum:
guesses -=1
print("wrong," + str(guesses) + "guesses left")
guess = input("guess again ")
if guesses <= 1:
print("You lose")
break
if guess == randomNum:
print("You win")
break
print guessNum()
so im having issues with it saying false when its correct. Also when I create a function, it only executes if I give it an input. why does it require an input? cant it have 0 or guessnum(1) and put a random variable when defining it, ie def guessnum(num):
The problem is that when you input something, it isn't normally an integer. Instead, it's a string. Hence, all you have to do is add the line guess = int(guess) right after the user guesses again.
I have a program that prompts the user for a guess on a guessing game, and I need the program to keep the values stored some how in order to prevent the user from guessing again.
For instance if a user inputs 'a' for the first guess and 'b' for the second, I need the program to store these and tell tell the user that they have already been used.
Guesses are always single characters. I am not allowed to use lists, dictionaries, or sets.
I know that i should concatenate the strings, but other than I'm clueless. I want to use a if statement or a function but not sure how to set those up.
I am also thinking that i need something like variable += variable. Any tips?
As mentioned in the comments, you should really be using a set. Here's how to do it with string concatenation though.
guesses = ""
guess = input("guess: ")
if guess in guesses:
print("already used")
else:
guesses += guess
You could use a set and a simple while-loop:
guesses = set()
while True:
guess = input("Please enter a guess: ")
if guess in guesses:
print("Sorry you have already guessed: %s" % guess)
else:
print("You guessed: %s" % guess)
guesses.add(guess)
Example Usage:
Please enter a guess: 5
You guessed: 5
Please enter a guess: 5
Sorry you have already guessed: 5
Please enter a guess:
Try it here!
If you are using C# you can use a StringBuilder (using System.Text;)
StringBuilder guesses = new StringBuilder();
guesses.Append("a");
bool hasGuess = guessese.ToString().Contains("a");
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 7 years ago.
import random
print("Hey there, player! Welcome to Emily's number-guessing game! ")
name=input("What's your name, player? ")
random_integer=random.randint(1,25)
tries=0
tries_remaining=10
while tries < 10:
guess = input("Try to guess what random integer I'm thinking of, {}! ".format(name))
tries += 1
tries_remaining -= 1
# The next two small blocks of code are the problem.
try:
guess_num = int(guess)
except:
print("That's not a whole number! ")
tries-=1
tries_remaining+=1
if not guess_num > 0 or not guess_num < 26:
print("Sorry, try again! That is not an integer between 1 and 25! ")
break
elif guess_num == random_integer:
print("Nice job, you guessed the right number in {} tries! ".format(tries))
break
elif guess_num < random_integer:
if tries_remaining > 0:
print("Sorry, try again! The integer you chose is a litte too low! You have {} tries remaining. ".format(int(tries_remaining)))
continue
else:
print("Sorry, but the integer I was thinking of was {}! ".format(random_integer))
print("Oh no, looks like you've run out of tries! ")
elif guess_num > random_integer:
if tries_remaining > 0:
print("Sorry, try again! The integer you chose is a little too high. You have {} tries remaining. ".format(int(tries_remaining)))
continue
else:
print("Sorry, but the integer I was thinking of was {}! ".format(random_integer))
print("Oh no, looks like you've run out of tries! ")
I'll try to explain this as well as I can... I'm trying to make the problem area allow input for guesses again after the user inputs anything other than an integer between 1 and 25, but I can't figure out how to. And how can I make it so that the user can choose to restart the program after they've won or loss?
Edit: Please not that I have no else statements in the problems, as there is no opposite output.
Use a function.Put everything in a function and call the function again if the user wants to try again!
This will restart the complete process again!This could also be done if the user wants to restart.
Calling the method again is a good plan.Enclose the complete thing in a method/function.
This will solve the wrong interval
if not guess_num > 0 or not guess_num < 26:
print("Sorry, try again! That is not an integer between 1 and 25! ")
continue
For the rest, you can do something like this
create a method and stick in your game data
def game():
...
return True if the user wants to play again (you have to ask him)
return False otherwise
play = True
while play:
play = game()