Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 3rd, 2005, 8:24 AM   #1
shak187
Newbie
 
Join Date: May 2005
Posts: 2
Rep Power: 0 shak187 is on a distinguished road
converting python to java

hi there i am having prblems with conveting a python program into java i was hoping someone can help me reply bck if you wnt to see the coding thank you
shak187 is offline   Reply With Quote
Old May 3rd, 2005, 8:31 AM   #2
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
you shouyld always put te code when asking for help....
Berto is offline   Reply With Quote
Old May 3rd, 2005, 1:06 PM   #3
shak187
Newbie
 
Join Date: May 2005
Posts: 2
Rep Power: 0 shak187 is on a distinguished road
sorry here is the code

from random import *

GAVE_UP = -99
MAX_GOES = 15
LOWER_EASY = 0
UPPER_EASY = 100
FIRST = True
OTHER = False

def main():
numGoes = 0 #number of times the game is played
totalGoes = 0 # total number of guesses made
start = True
numCorrect = 0

while numGoes < MAX_GOES and start:
num = randint(LOWER_EASY, UPPER_EASY)
# print "For testing purposes the num generated is:",num
guess = getGuess(FIRST)
numGuesses = 1 # number of guesses made in this game
numGoes += 1
carryOn = True

while guess != num and carryOn:
carryOn = tryAgain(guess, num)
if carryOn:
guess = getGuess(OTHER)
numGuesses += 1

if guess == num:
print "Great! You got it right in " + str(numGuesses) + " goes!"
numCorrect += 1

totalGoes += numGuesses

start = checkAgain()

outputResults(numCorrect, numGoes, totalGoes)


def getGuess(begin):
guess = 0 # why do I need to do this?
message = ""
if begin:
message = "I've picked a number between " + str(LOWER_EASY) + " and " + str(UPPER_EASY) + "! What do you think it is? >>> "
else:
message = "What's your next guess? >>> "
guess = input(message)
return guess


def tryAgain(gen, user):
retValue = True
compare = "low"
if gen > user:
compare = "high"

ret = raw_input("Your guess is too " + compare +". Do you want to guess again (y/n)? >>> ")
while not validResponse(ret):
ret = raw_input("Sorry I don't recognise your input.\nDo you want to guess again (y/n)? >>> ")
if ret == "N" or ret == "n":
print "Quitter!!!"
retValue = False
return retValue


def checkAgain():
retValue = True
ret = raw_input("That Game's over.... Do you want to play again (y/n)? >>> ")
while not validResponse(ret):
ret = raw_input("Sorry I don't recognise your input.\nDo you want to play again (y/n)? >>> ")
if ret == "N" or ret == "n":
retValue = False
return retValue


def outputResults( correct, timesPlayed, totalAttempts):
print "Thanks for playing.. hope you had fun..."
print "You played", timesPlayed, "times."
print "You guessed correctly", correct, "times, and you gave up", timesPlayed - correct, "times"

def validResponse( ret):
ok = False
if ret == "Y" or ret == "y" or ret == "N" or ret == "n":
ok = True
return ok


main()
shak187 is offline   Reply With Quote
Old May 4th, 2005, 12:44 AM   #4
ZenMasterJG
Hobbyist Programmer
 
ZenMasterJG's Avatar
 
Join Date: Nov 2004
Location: Boston, MA
Posts: 148
Rep Power: 4 ZenMasterJG is on a distinguished road
Send a message via AIM to ZenMasterJG
and how about the Java code you've tried?
ZenMasterJG is offline   Reply With Quote
Old May 4th, 2005, 2:36 AM   #5
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
well all definitions can be turned into functions start from there...
Berto is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:20 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC