Hi, I have a problem with implementing a for loop for my program. I don't understand how to correctly make a for loop in asking the player to play again after getting the number right. The options should be yes as 1 and no as 2.
I have this so far...: -
def playgame():
-
import random
-
num = random.randint(1,10)
-
guess = 0
-
score = 0
-
name= input(('Hi, what is your name?'))
-
print('Hi', name,'. Try to guess my number from 1-10!')
-
while(guess != num):
-
guess = int(input('GUESS MY NUMBER!'))
-
if(num < guess):
-
print('You guessed..', guess)
-
print('THE NUMBER IS TOO HIGH!')
-
score = score+1
-
print ('Your score so far is..', score)
-
if(num > guess):
-
print('You guessed..', guess)
-
print('THE NUMBER IS TOO LOW!')
-
score = score+1
-
print ('Your score so far is..', score)
-
if(num == guess):
-
print('You guessed..', guess)
-
print('YOU ARE CORRECT!')
-
print('Your final score is..', score)
-
-
playgame()
3 3025 bvdet 2,851
Expert Mod 2GB
You should make your import statements at the very top of the code listing. I have not tested it, but something like the following might work for you. - import random
-
-
def playgame():
-
score = 0
-
while True:
-
num = random.randint(1,10)
-
guess = 0
-
name= input(('Hi, what is your name?'))
-
print('Hi', name,'. Try to guess my number from 1-10!')
-
while(guess != num):
-
guess = int(input('GUESS MY NUMBER!'))
-
if(num < guess):
-
print('You guessed..', guess)
-
print('THE NUMBER IS TOO HIGH!')
-
score = score+1
-
print ('Your score so far is..', score)
-
if(num > guess):
-
print('You guessed..', guess)
-
print('THE NUMBER IS TOO LOW!')
-
score = score+1
-
print ('Your score so far is..', score)
-
if(num == guess):
-
print('You guessed..', guess)
-
print('YOU ARE CORRECT!')
-
print('Your final score is..', score)
-
if int(input('(1)Play again, (2)Exit')): == 2
-
return
-
-
playgame()
Thank you for responding! There was some indentation errors I fixed, but now line 26 has a syntax error..
bvdet 2,851
Expert Mod 2GB
It looks like I left off the colon (:).
Sign in to post your reply or Sign up for a free account.
Similar topics
by: EAS |
last post by:
Hey, I'm new to python (and programming in general) so I'll prolly be around
here a lot...
Anyways, I've found out how to make a "guess my number game" where the
player guesses a number between...
|
by: Alex Endl |
last post by:
ok now that i know the random function, i made this guessing game. I get an
error though, and Im new so im not to good at figuring out what its talking
about.
import random
a =...
|
by: raypjr |
last post by:
Hi everyone. I need a little help with some parts of a word guessing game I'm working on. I have some parts done but unsure about others and could use a little advice. Any help is very much...
|
by: moopower |
last post by:
I have to do the following:
For this project, you are to create a simple number guessing game. The program should
begin by asking the user for a range (i.e. a minimum number and a maximum number)...
|
by: Kraken |
last post by:
Hi, i have a bit of a problem here.
I have an assignment to do an animal guessing game using an original database and updating it as the user enters new animals in it. The program enters the file...
|
by: Gomi |
last post by:
Hi guys I'm new to C++ Programming and I am having trouble in making my Guessing game code replay. I am suppose to give the user that is playing my game the opportunity to play again with options of...
|
by: KoreyAusTex |
last post by:
I have written a program using method calls as opposed to nested while loops, or rather a while within a while, but for some reason the only way I can get the program to terminate is to type break in...
|
by: falconsx23 |
last post by:
Hi, I am making a guessing game from the java language. The user is asked to input a number. The number is 1 through a random max number. If the number guessed is correct, then the program should...
|
by: sbdalecia |
last post by:
In C++ I have to create a guessing game that has three loops and and I have to make a flowchart. Now mind you I am a beginner at all of this. Someone help!
|
by: rockchicx23 |
last post by:
i have to write a program that is a guessing the number game. the user has to choose the number of games they want to play. i need help setting up the loop that will run the number of times the user...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |