![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2005
Posts: 2
Rep Power: 0
![]() |
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
|
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
you shouyld always put te code when asking for help....
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: May 2005
Posts: 2
Rep Power: 0
![]() |
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() |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
and how about the Java code you've tried?
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
well all definitions can be turned into functions start from there...
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|