Hey I'm trying to add a variable for "won" or "loss", I already have a variable for players name and guesses allowed.
Any help would be kind thanks. This is the code I have so far:
import random
number = random.randint(1, 100)
player_name = input("Hello, What's your name?: ")
number_of_guesses = 0
print("Okay! "+ player_name+ " I am guessing a number between 1 and 100:")
max_guesses = random.randint(1, 6)
print("You have " + str(max_guesses) + " guesses. ")
while number_of_guesses < max_guesses:
guess = int(input())
number_of_guesses += 1
if guess < number:
print("Your guess is too low")
if guess > number:
print("Your guess is too high")
if guess == number:
break
if guess == number:
print("You guessed the number in " + str(number_of_guesses) + " tries!")
else:
print("You did not guess the number, the number was " + str(number))
f = open("statistics.txt", "a")
f.write =(player_name) (max_guesses)
f.close()
f = open("statistics.txt", "r")
print(f.read())
Maybe add befor you loop the variable won = False
And in the loop
if guess == number:
won = True
break
After the loop if the player don't find the nulber won will be false.
In the oter case it will be True
For saving
f.write( str(won) ) # convert to string
Related
So I'm refreshing what little I knew of Python before and playing around with some beginner projects. I'm toying arond with it now, and I'm just trying to learn and see what I can do. I made a "Guessing Game" and turned it into a function. I want to store these reults in a list each time it is used. I want the results to automatically go to the list when the game is completed and then to be able to print the list when desired.
I'm not sure if I need to create a new function for this, or if I should be creating this within my current "guessing_game" function. I've tried to create a list previously, but I'm not sure how to create and store the variable of the game result in order to add it into the list. I feel like this is probably a fairly simple problem, so I apologize if this is a dumb question.
def guessing_game():
import random
number = random.randint(1, 1000)
player_name = input("Enter name ")
number_of_guesses = 0
print('Howdy' + player_name + "Guess a number between 1 and 1000: ")
while number_of_guesses < 10:
guess = int(input())
number_of_guesses += 1
if guess < number:
print("Too Low, Joe")
if guess > number:
print("Too high, Sly")
if guess == number:
break
if guess == number:
print("You got it, Bobbit, in " + str(number_of_guesses) + " tries")
else:
print(" Well, yer were close, Stofe. Shoulda guessed " + str(number))
print(guessing_game())
You can create a list inside of the function, and then everytime they guess, you can store the guess inside the list. At the end, we can print the list.
def guessing_game():
import random
number = random.randint(1, 1000)
player_name = input("Enter name: ")
number_of_guesses = 0
guesses = []
print('Howdy ' + player_name + " Guess a number between 1 and 1000: ")
while number_of_guesses < 10:
guess = int(input())
guesses.append(guess)
number_of_guesses += 1
if guess < number:
print("Too Low, Joe")
if guess > number:
print("Too high, Sly")
if guess == number:
break
if guess == number:
print("You got it, Bobbit, in " + str(number_of_guesses) + " tries")
else:
print(" Well, yer were close, Stofe. Shoulda guessed " + str(number))
print("These were the numbers you guessed:")
for g in guesses:
print(g)
print(guessing_game())
What you need to do is create a list with something like val = []. I added it into the code and also added a formatting piece so it looks nice.
def guessing_game():
import random
guesses = []
number = random.randint(1, 1000)
guess = number / 2
player_name = input("Enter name ")
number_of_guesses = 0
print(f'Howdy {player_name}. Guess a number between 1 and 1000: ')
while number_of_guesses < 10:
guess = int(input('> '))
guesses = guesses + [guess]
number_of_guesses += 1
if guess < number:
print("Too Low, Joe")
if guess > number:
print("Too high, Sly")
if guess == number:
break
formatted_guesses = ''
for _ in range(len(guesses)):
if _ != len(guesses) - 1:
formatted_guesses = formatted_guesses + str(guesses[_]) + ', '
else:
formatted_guesses = formatted_guesses + str(guesses[_]) + '.'
if guess == number:
print("You got it, Bobbit, in " + str(number_of_guesses) + " tries")
print(f'You guessed {formatted_guesses}')
else:
print("Well, yer were close, Stofe. Shoulda guessed " + str(number))
print(f'You guessed {formatted_guesses}')
guessing_game()
I've created a python guessing game but now I want to be able to save the game statistics to a file at the end of each game. For each game the statistics to be recorded include the player name, their play status(win or loss) and the number of tries. I need the game to be saved in this format:
Username|status|number of guesses
If anyone could help me with that, that would be kind.
This is the code I have for the game:
import random
number = random.randint(1, 100)
player_name = input("Hello, What's your name?: ")
number_of_guesses = 0
print("Okay! "+ player_name+ " I am guessing a number between 1 and 100:")
max_guesses = random.randint(1, 6)
print("You have " + str(max_guesses) + " guesses. ")
while number_of_guesses < max_guesses:
guess = int(input())
number_of_guesses += 1
if guess < number:
print("Your guess is too low")
if guess > number:
print("Your guess is too high")
if guess == number:
break
if guess == number:
print("You guessed the number in " + str(number_of_guesses) + " tries!")
else:
print("You did not guess the number, the number was " + str(number))
f = open("Statistics.txt", "a")
f.write(player_name), (number_of_guesses)
f.close()
f = open("Statistics.txt", "r")
print(f.read())
A random number between 1 and 6, this represents a roll of a dice. The random number becomes the number of allowed guesses from the user. I cannot get the dice number to be same as amount of guesses allowed.
This is what I have so far:
import random
number = random.randint(1, 100)
player_name = input("Hello, What's your name?")
number_of_guesses = 0
print('okay! '+ player_name+ ' I am guessing a number between 1 and 100:')
min_value = 1
max_value = 6
print(random.randint(min_value, max_value))
while number_of_guesses < 5:
guess = int(input())
number_of_guesses += 1
if guess < number:
print("Your guess is too low")
if guess > number:
print("Your guess is too high")
if guess == number:
break
if guess == number:
print("You guessed the number in " + str(number_of_guesses) + " tries!")
else:
print("You did not guess the number, the number was " + str(number))
OK, this should fix your issues:
import random
number = random.randint(1, 100)
player_name = input("Hello, What's your name?")
number_of_guesses = 0
print('okay! '+ player_name+ ' I am guessing a number between 1 and 100:')
max_guesses = random.randint(1, 6)
print(f"You have {max_guesses} tries. ")
won = False
while number_of_guesses < max_guesses:
guess = int(input())
number_of_guesses += 1
if guess < number:
print("Your guess is too low")
if guess > number:
print("Your guess is too high")
if guess == number:
won = True
break
if won:
print("You guessed the number in " + str(number_of_guesses) + " tries!")
else:
print("You did not guess the number, the number was " + str(number))
However, this seems like some starting-out project, so it is important to understand what every code line is doing. In case you are not sure about anything, ask away :)
It looks like you're printing the random number, but not really using it. Could you try the following:
import random
number = random.randint(1, 100)
player_name = input("Hello, What's your name?")
number_of_guesses = 0
print('okay! '+ player_name+ ' I am guessing a number between 1 and 100:')
min_value = 1
max_value = 6
random_number = random.randint(min_value, max_value)
print(random_number)
while number_of_guesses < random_number:
guess = int(input())
if guess < number:
print("Your guess is too low")
if guess > number:
print("Your guess is too high")
if guess == number:
break
number_of_guesses += 1
if guess == number:
print("You guessed the number in " + str(number_of_guesses) + " tries!")
else:
print("You did not guess the number, the number was " + str(number))
Trying my first Python game with Python 3.6. Have tried several IDEs including Geany, and Wing Personal.
The error is:
Traceback (most recent call last):
File "game1.py", line 7, in
myName = input()
File "", line 1, in
#This is a guess the numbers game.
import random
guessesTaken = 0
print("Hello! What is your name?")
myName = input()
number = random.randint(1, 20)
print("Well, " + myName + ", I am thinking of a number between 1 and 20.")
while guessesTaken < 6:
print("Take a guess.")
guess = input()
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print("Your guess is too low.")
if guess > number:
break
if guess == number:
guessesTaken = str(guessesTaken)
print("Good job, " + myName + "! You guesses my number in " + guessesTaken + " guesses!")
if guess != number:
number = str(number)
print("Nope. The number I was thinking of was " + number)
This code has multiple indentation errors. Indentation generally means four spaces or one tab character. So, the code inside each if-else condition should be indented by one level.
Also, in one of the conditions:
if guess > number:
break
What do you expect that to do? Break is used to stop the loops. I guess you want to exit the program for this particular case. For that you can use return 0 or sys.exit().
Here is the code, properly indented:
#This is a guess the numbers game.
import random
import sys
guessesTaken = 0
print("Hello! What is your name?")
myName = input()
number = random.randint(1, 20)
print("Well, " + myName + ", I am thinking of a number between 1 and 20.")
while guessesTaken < 6:
print("Take a guess.")
guess = input()
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print("Your guess is too low.")
if guess > number:
sys.exit()
if guess == number:
guessesTaken = str(guessesTaken)
print("Good job, " + myName + "! You guesses my number in " + guessesTaken + " guesses!")
if guess != number:
number = str(number)
print("Nope. The number I was thinking of was " + number)
This is a game I am currently trying to make. I am coding this game in python 3.4. it doesn't run.
# this is a guess the number game!
import random
guesses = 0
name = input("what is your name?")
number = random.randint(1, 20)
print = name + ", I am thinking of a number between 1 and 20..."
while guesses << 7:
guess = int(raw_input("Take a guess."))
guesses = guesses + 1
if guess < number:
print ("your guess is too low!")
if guess > number:
print ("your guess is too high!")
if guess == number:
break
if guess == number:
guesses = str(guesses)
print ("Good job," + name + "you guessed my number in" +guesses +"guesses!")
if guess != number:
number = str(number)
print ("Nah dude, better luck next time!")
I think you meant to use < instead of <<. << and >> are bit shift operators to the left and right respectively.
Your last two if conditions are also outside your loop, and don't make much sense. You're already checking if guess == number once and breaking if that condition is met. if guess != number your already checking this by using < and > respectively.
print = ...? print syntax is print(some_stuff, ...). Indentation is also off at the top, but assuming that's just due to posting your first question.
Also, raw_input is for python2 it's just input in python3. You could clean the print statements up some with % formatters or using .format.
Fixed code: (Python 3 version since that's whats tagged in the question...)
import random
name = input("what is your name?")
number = random.randint(1, 20)
#print("%s I am thinking of a number between 1 and 20..." % name)
print(name + " I am thinking of a number between 1 and 20...")
guesses = 0
while guesses < 7:
guess = int(input("Take a guess."))
guesses += 1
if guess < number:
print ("your guess is too low!")
elif guess > number:
print ("your guess is too high!")
else:
#print("Good job %s you guessed my number in %d guesses" % (name, guesses))
print ("Good job, " + name + " you guessed my number in " + str(guesses) + " guesses!")
break
There are many errors in your program. Always include errors you get in your question. Given the syntax error you are making first get your hands dirty on python interpreter by executing simple commands. Below should help. Below is in Python 2, for Python 3 replace, raw_input() with input and print 'something' with print ('something')
1st Solution:
import random
name = raw_input("Hello! What is your name?\n")
print "Well, " + name + ", I am thinking of a number between 1 and 20"
no = random.randint(1,20)
guess = int(raw_input("Take a guess\n"))
count =1
while guess != no:
if guess < no:
print "Your guess is too low."
if guess > no:
print "Your guess is too high"
count +=1
guess = int(raw_input("Take a guess\n"))
print "Good job, %s! You guessed my number in %d guesses!" % (name ,count)
2nd Solution:
import random
def check():
global count # good example of use of global
guess = int(raw_input("Take a guess\n"))
if guess == no:
print "Good job, %s! You guessed my number in %d guesses!" %(name,count)
elif guess < no:
print "Your guess is too low."
count +=1
check()
else:
print "Your guess is too high"
count +=1
check()
name = raw_input("Hello! What is your name?\n")
print "Well, " + name + ", I am thinking of a number between 1 and 20"
no = random.randint(1,20)
global count
count =1
check()
Your code goes good, little changes can make it run!
import random
guesses = 0
name = raw_input("what is your name?") # use input() is using Python 3
number = random.randint(1, 20)
print name + ", I am thinking of a number between 1 and 20..."
while guesses < 7:
guess = int(raw_input("Take a guess."))
guesses = guesses + 1
if guess < number:
print ("your guess is too low!")
if guess > number:
print ("your guess is too high!")
if guess == number:
break
if guesses == number:
print ("Good job,", name, "you guessed my number in", guesses, "guesses!")
if guesses != number:
number = str(number)
print ("Nah dude, better luck next time!", "The number is", number)