Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8796419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:37:08+00:00 2026-06-13T23:37:08+00:00

Below is the Python code for a simple game i had to create now

  • 0

Below is the Python code for a simple game i had to create now i am having difficulty seperating the code to play the game over a client and server using sockets. any help would be appreciated

import random
rannum = random.randrange(0,10,1)
print rannum
counter = 0
a=0

while(a==0):
    guess = int(raw_input("What is your guess?: "))
    counter = counter + 1

    diff = abs(guess - rannum)
    if(diff >5):
        print "Way Off!"
    elif(diff >2):
        print "Close!"
    elif (diff>1):
        print "Closer!"
    elif(diff >0):
        print "Even Closer!"

    elif (diff==0):
        print "Correct"
        name = str(raw_input("What is your name?: "))
        print name + " Your score is: "
        print counter
        f = open('high_scores.txt', 'a')
        score = str(counter)
        f.write(name + " " + score + "\n")
        f.close()
        break
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:37:10+00:00Added an answer on June 13, 2026 at 11:37 pm

    The client interface in your code is just the raw_input function and print statements. Effectively, all you need to do is turn those into recv and send calls on a socket, and you’ve got a server. Or, more simply, wrap the socket in makefile and just write and read lines:

    import random
    import socket
    rannum = random.randrange(0,10,1)
    print rannum
    counter = 0
    a=0
    
    sock = socket.socket()
    sock.bind(('127.0.0.1', 12345))
    sock.listen(5)
    clientsock, addr = sock.accept()
    client = clientsock.makefile('r+')
    
    while(a==0):
    client.write('What is your guess?: ')
    guess = int(client.readline())
    counter = counter + 1
    
    diff = abs(guess - rannum)
    if(diff >5):
        client.write("Way Off!\n")
    
    …
    

    You can test this without even writing a client, just using nc localhost 12345. (If you’re on Windows, you don’t have nc built-in; google “netcat Windows” and get yourself a copy.) Then just look at the docs for socket.connect and you should be able to write the client side.

    Now, you may want to reorganize your interface a bit. Maybe the client should provide the prompts and format the results (so, e.g., you can have separate English and Spanish clients with the same server—or, even better, a nice GUI client). To do that, you need to design some kind of protocol for the client and server to use for communication. (In fact, even the trivial example above has an implicit protocol: every prompt, response, etc. is separated by newlines. I “cheated” by using makefile and readline to handle that automatically. But figuring out how to separate messages—newlines, length-prefixing, etc.—is just the first step.)

    You also may want to consider whether you really want to design a “raw” protocol that sits on top of TCP, or use something that takes care of the low-level stuff for you (a web service, JSON-RPC over sockets, etc.) so you only need to write the application-specific stuff yourself.

    Finally, this is a very, very bad server. It only accepts one client, and it does everything synchronously, so if the client screws up the server can’t recover. A real server needs what’s called a “reactor” or a “proactor” (you can built your own with select.select, or use asyncore or something third-party like twisted), or needs to spawn a new thread or process for each accept.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a simple HTTP client and server in Python for experimenting. The first
I have a python psp page code is shown below. Currently it only prints
In the code shown below, I need to manipulate the time var in python
I am trying to implement this package http://code.google.com/p/python-progressbar/ when downloading a file like below:
I'm using python 3.3.0 in Windows 8. Below code successfully runs in python 2.7.x
I have wrote following code for a simple problem in Python - def Peu1(numbers):
I'm trying to develop simple Python (3.2) code to read XML files, do some
Please see the code below:- #!/usr/bin/python # Filename: total.py def total(initial=5, *numbers, **keywords): count
I have some code (see below) that works as expected under Python 2, but
Below is some sample code. The window does not resize (python3.2, on a Mac,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.