So my professor told me to make a number guessing game. I did that but she later uploads guidelines on how she want's it done. My code is so different and I don't know how to edit it to match her expectation. Please help.
Player One picks a number and Player Two has 5 Guesses to guess it. If he manages to do so, he wins, if not, he losses and Player One wins.
If for example Player one picks the number '3' and Player Two enters the number '3' on any of his goes, it still says Player One wins.
This is my code and my attached assignment photo
def Game():
Guess = 0
NumberOfGuesses = 0
NumberToGuess = int(input("Player One enter you chosen number: "))
while NumberToGuess < 1 or NumberToGuess > 10:
NumberToGuess = int(input("Not a valid choice, please enter another number: "))
while Guess != NumberToGuess and NumberOfGuesses < 5:
Guess = int(input("Player Two have a guess: "))
NumberOfGuesses = NumberOfGuesses + 1
if Guess == NumberToGuess:
print("Player Two wins.")
else:
print("Player One wins.")
Game()
my assignment
So the assignment is pretty clear in my opinion.
This is a turn-based PvP Game with guessing a random number.
Your main_game loop needs the parameters on how many rounds are played.
Every round consists of 2 game_steps, which include 2 tries to guess the number + some I/O Stuff that is described in your assignment. Guessing on the first try rewards 5 points, and guessing on the second try rewards 3 points.
Personally, I refrain from using while loops unless for very specific use cases. In my opinion, for loops are just better. You can iterate over lists, ranges, dataframes, etc., and easily unpack data while you are at it.
The next thing I would recommend is to stick to the PEP-8 pythonic naming convention. It just makes it easier to read the code. Whenever I see a capital letter in a python script, I instantly think of a class. And later in the workplace, it is extremely important that code is easily readable.
If I were you, I would discard your code and start from scratch, I don't think there is much to salvage from the code you provided. Just read your assignment carefully and think about how you would solve it logically before starting to code.
Related
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed last year.
Random Number Generator - Guess It
Did a much more complex variant of this. (250+ lines) But when I tested it, even if I guessed the number correctly(it was between 1-20) it didn't accept it. Every stage, I programmed the computer to tell me a statement about the number, using if, elif and else commands. When I guessed the number, it just said the number is incorrect and told me a new statement about number. Could someone help, please? In this mini variant as well, if I get the correct answer, it still responds to the if statement.
import random
list = [1,2,3]
target = print(random.choice(list))
guess = input("What is the target number?")
if guess == target :
print("Well done.")
else:
print("The number you have guessed isn't correct.")
You need to convert the guess into an integer before comparison. and remove the print statement from the variable definition. You also need to rename the list variable as it is a protected name.
import random
listNumbers = [1,2,3]
target = random.choice(listNumbers)
guess = int(input("What is the target number?"))
if guess == target :
print("Well done.")
else:
print("The number you have guessed isn't correct.")
I am trying to learn simple python. Just testing myself with a simple code before moving on.
My goal is for the program to figure out your average grade, and tell you if it is good or not. Very simple. Problem occurs when typing using the code, the code will under almost every circumstance say your grade is good. If I enter every grade I have as 2, the average grade would clearly be 2. Yet the program says the grade is good.
I initially thought it was a problem with the program reading the inputs as strings. Therefore I tried clarifying that it is supposed to be read as a float. This very well might still be a problem, as I have maybe not clarified it in the right spot of the program. I have tried switching between ">=" and "<=" in my if statement.
Im sure the solution is very simple, but I can not figure it out by myself right now. Kind of expecting myself to facepalm at how easy it is.
Anyway, here is the code:
#Making user type in different grades
math_grade = float(input("Type your math grade here: "))
english_grade = float(input("Type your english grade here: "))
gym_grade = float(input("Type your gym grade here: "))
#Figuring out average grade
average = math_grade + english_grade + gym_grade / 3
#Attempting to make every grade 3 or under "bad" and everything above "good."
if average <= 3:
print("Your average grade is bad!")
else:
print ("Your average grade is good!")
Very little code so should not be hard to copy/paste it and experiment with inputs yourself. Thank you for whatever help I get :)
Just a simple issue with the order in which Python completes the mathematical equation. Just put the additions in brackets when calculating the average.
average = (math_grade + english_grade + gym_grade) / 3
This question already has answers here:
How does my input not equal the answer?
(2 answers)
Closed 5 years ago.
Hi all I hope you can explain what I am doing wrong. I am completely new to python and experimenting with basic code but it don't seem to work as i thought it might.
the program should be a simple one of guessing the right number contained in the variable, but even when guess correct it says "Nope, that's not right".
magic_number = 10
input("I am thinking of a number between 1 and 10, can you guess it? ")
if input == magic_number:
print("WOW! You must be psychic, that is spot on")
else:
print("Nope, that's not it")
first of all you should creat a variable that will store the answer, so for example.
answer = input("I am thinking of a number between 1 and 10, can you guess it? ")
thus you will also have to change if input == magic_number: to if answer == magic_number:
Nonetheless, the main problem is that when you enter something using the input method your input is automatically converted to a string.
So you have two choices:
Convert your magic number to a string (magic_number = 10to magic_number = "10") and run the code with the modifications that I have proposed
Convert your input too and Int modifying if answer == magic_number: to if int(answer) == magic_number:
Both of the methods work great, though be careful with the second one because if you input something that is not convertible to an int (for example "Hello", the code will return an error)
input isn't a variable, it's a function! You should type x=input("Enter a number"), and if x == magic_number. input returns a value, but you aren't storing the user's input anywhere.
try this :
magic_number = 10
guess = int(input("I am thinking of a number between 1 and 10, can you guess it? "))
if guess == magic_number:
print("WOW! You must be psychic, that is spot on")
else:
print("Nope, that's not it")
I'm doing a controlled assessment on python. One of the tasks is to create a vending machine under certain criteria. I'm pretty bad a python and I'm probably being an idiot and doing this wrong.
I want the user to input only 10,20,50,1.00 coins. If the user enters anything other than these coins, I want it to print "Machine doesn't accept these coins".
This is what I have so far:
inp = input("Enter Coins, Note: Machine only accepts 10, 20, 50 and 100 coins: ")
value = [10,20,50,100]
if inp != value:
print("Machine doesn't accept these coins")
else:
print("What would you like to buy?")
Here, you want:
if any(int(coin) not in value for coin in inp.split()):
print("Machine doesn't accept these coins")
What this basically does it split up the input into separate coins, converts them to integers, (because the items in values are integers) then checks if it is not in values, which of course would mean it is invalid.
Finally, this is done until it finds an invalid coin (take a look at any). At that, it will print that the coins are invalid. If it does not, then it will continue to else.
I'm new to Python as part of an exercise I'm making a simple number guessing game. I've gotten the basics covered, but I'm trying to implement a manner which tells the user if their guess is close or far from the right number. I can't guess within a range because the number to be guessed is randomly selected each time. My exercise tells me to look into abs(), but that doesn't bring me much sense. It also mentions something about within(), which also doesn't do much for me. Just need a push in the right direction if anyone can help. Thanks.
You have to come up with values that mean close or far.
It might look something like this:
diff = abs(guess - random_number)
if diff >= 50:
print("Really cold!")
elif diff >= 40:
print("Cold.")
...
elif diff >= 5:
print("Getting really hot!")
You could have a function return a number which represents how hot or cold it is (0 through 10 for example), then have a dict that looks like this:
hot_str = {
0: "You guessed it!",
1: "Extremely hot!",
10: "Frozen cold!",
}
print(hot_str[heat(diff)])
You could use operator chaining and test against both extremes of your range:
if lower <= number <= upper:
This test matches if number is in the range [lower, upper] (both ends inclusive).
If you wanted to see how close a value is to something else, you could use abs(target - guess):
distance = abs(target - guess)
if 10 <= distance <= 20:
print('You are getting closer now!')
A whole series of such tests are going to be tedious; you can print messages based on how close they are with a sequence of tests:
messages = (
(100, 'Way, way off!'),
(80, 'So cold, are you not freezing?'),
(60, 'Is there a light on the horizon?'),
(40, 'Where there is warmth, there is hope!'),
(30, 'You are getting warmer now..'),
(20, 'Is it just me, or is it getting hot in here?'),
(10, 'You are about to burn yourself!'),
(0, 'Fire, ta-cha-ta, burning desire, ta-cha-ta!')
)
distance = abs(target - guess)
if distance == 0:
print("You guessed it right!")
else:
for target, message in messages:
if distance > target:
print(message)
break
It depends on how you define close and far. For now I assume let d be parameter and if abs(guess-right_num) is less than d then it is close otherwise it is far
so the code can be:
if abs(guess-right_num) < d:
print("close")
else:
print("far")
You can't use a range to directly compare your number and the randomly generated number, but you can use a range to directly compare their differences.
abs() can be used to return the positive version of a number whether it is positive or negative.
abs(10)
10
abs(-10)
10
You can use this to ensure your random number is always positive.
You must be misreading about within(); This is either something accessible only to you and your class, or you misread what it meant, because within() is not a build-in function.
Now, on solving your problem. This takes a bit of math and logic.
You can't work within a range because the number is randomly generated. But you can always get a percentage of them and work based on that.
As an example, consider this number line, where the random number is 10 (denoted by |).
[(0)---------|---------(20)]
You will always be able to divide the number you guess by the random number and get a number that will range from (in this case) (0/10) and (10/20). In all cases, when number_guessed == number_generated, the number calculated in the division operation will be equal to 1.
I leave you to figure out the implementation of this, but the basic concept is that you can figure out how close you are to the number based on how far away you are from the number 1.
It's not perfect either, but it's an interested and clever way that might get a professor's notice, if you do it right.