473,326 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How do I make a for loop for my guessing game?

2
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...:
Expand|Select|Wrap|Line Numbers
  1. def playgame():
  2.     import random
  3.     num = random.randint(1,10)
  4.     guess = 0
  5.     score = 0
  6.     name= input(('Hi, what is your name?'))
  7.     print('Hi', name,'. Try to guess my number from 1-10!')
  8.     while(guess != num):
  9.             guess = int(input('GUESS MY NUMBER!'))
  10.             if(num < guess):
  11.                 print('You guessed..', guess)
  12.                 print('THE NUMBER IS TOO HIGH!')
  13.                 score = score+1
  14.                 print ('Your score so far is..', score)
  15.             if(num > guess):
  16.                 print('You guessed..', guess)
  17.                 print('THE NUMBER IS TOO LOW!')
  18.                 score = score+1
  19.                 print ('Your score so far is..', score)
  20.             if(num == guess):
  21.                 print('You guessed..', guess)
  22.                 print('YOU ARE CORRECT!')
  23.                 print('Your final score is..', score)
  24.  
  25. playgame()
Feb 2 '13 #1
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.
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. def playgame():
  4.     score = 0
  5.     while True:
  6.         num = random.randint(1,10)
  7.         guess = 0
  8.         name= input(('Hi, what is your name?'))
  9.         print('Hi', name,'. Try to guess my number from 1-10!')
  10.         while(guess != num):
  11.             guess = int(input('GUESS MY NUMBER!'))
  12.             if(num < guess):
  13.                 print('You guessed..', guess)
  14.                 print('THE NUMBER IS TOO HIGH!')
  15.                 score = score+1
  16.                 print ('Your score so far is..', score)
  17.             if(num > guess):
  18.                 print('You guessed..', guess)
  19.                 print('THE NUMBER IS TOO LOW!')
  20.                 score = score+1
  21.                 print ('Your score so far is..', score)
  22.             if(num == guess):
  23.                 print('You guessed..', guess)
  24.                 print('YOU ARE CORRECT!')
  25.                 print('Your final score is..', score)
  26.         if int(input('(1)Play again, (2)Exit')): == 2
  27.             return
  28.  
  29. playgame()
Feb 2 '13 #2
xxHana
2
Thank you for responding! There was some indentation errors I fixed, but now line 26 has a syntax error..
Feb 2 '13 #3
bvdet
2,851 Expert Mod 2GB
It looks like I left off the colon (:).
Feb 3 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
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...
6
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 =...
0
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...
4
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)...
5
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...
1
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...
9
KoreyAusTex
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...
6
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...
3
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!
1
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...
0
isladogs
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...
0
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...
0
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...
0
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...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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....
0
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
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.