While waiting sauna to heat up, i decided to study more python since it seems to be quiet in every darn forum there is on internet.
First, i created simple "guess the number" game with freebasic and then converted it to python.
For python programmers, my code prolly looks ugly as hell, but it is like my first > 5 lines long self-made code, so shut up :P
Anyway, after studying more python documents, it feels interesting language. Want to learn more eventho i still hate since cant use CAPS at anywhere :D Silly me.
guess.bas
' Guess what number for FreeBASIC .17b stable
' E.K.virtanen. 2007, public domain
Randomize TIMER
CONST MIN = 1
CONST MAX = 100
Dim AS Integer Plr_guess, Randomed, Rounds
' Main module of program.
DO
Color 15, 0 : CLS
Randomed = INT(RND * ((MAX + 1) - MIN) + MIN)
Rounds = 0
Print "Ok, i think random number between "; MIN; MAX; "."
Print "Your job is to guess what it is in as minimal tries as possible."
Print ""
Print "After your every guess, ill give you hint is my number higher or lower than your guess."
Print "You can exit by 'guessing' 0."
Print "Press any key to start game."; : GETKEY
' From here starts the gaming loop itself. This is looped until guess it right one.
CLS
DO
Rounds = (Rounds + 1)
Print "This is round number: "; Rounds; "."
INPUT "Give your guess: "; Plr_guess
Color 14, 0
IF Plr_guess > Randomed THEN
Print "My number is smaller..."
END IF
IF Plr_guess < Randomed THEN
Print "My number is higher..."
END IF
Color 15, 0
IF Plr_guess = Randomed THEN EXIT DO
LOOP While Plr_Guess <> 0
IF Randomed <> Plr_Guess THEN EXIT DO
Print "You got it!!!"
Print "It took "; Rounds; " rounds to guess right number."
Print ""
Print "Press 'y' to play again. Any other key to quit."
DO Until INKEY$ <> "" : Loop
IF INKEY$ <> "y" AND INKEY$ <> "Y" THEN EXIT DO
Loop
END
guess.py. Result of 2 hours python learning.
# Guess what number for python. My one of first python codes so it prolly looks ugly as hell.
# E.K.virtanen. 2007, public domain
# modules we need here
import random
import os
# function wich solves what OS is used. linux and windows regignized.
def os_clear():
return_value = "\n"
if os.name == "posix":
return_value = 'clear'
elif os.name in ("nt", "dos", "ce"):
return_value = 'CLS'
return return_value
# Constant variables
const_min = 1
const_max = 100
clear_screen = os_clear()
# MAIN MODULE OF PROGRAM
guess_game = end_game = 'play'
while end_game == 'play':
os.system(clear_screen)
randomed = random.randint(const_min, const_max)
rounds = 0
print
print 'Ok, i think random number between ', str(const_min), ' to ' + str(const_max)
print 'Your job is to guess what it is in as minimal tries as possible.'
print ''
print 'After your every guess, ill give you hint is my number higher or lower than your guess.'
print 'You can exit by "guessing" (0).'
raw_input('Press <ENTER> to start game.')
# guessing part of game
os.system(clear_screen)
while guess_game == 'play':
rounds += 1
print 'This is rounds number: ', str(rounds) + '.'
print
plr_guess = int(raw_input('Give your guess: '))
if int(plr_guess) < randomed:
print 'My number is higher...'
elif int(plr_guess) > randomed:
print 'Mu number is lower...'
elif int(plr_guess) == randomed:
guess_game = 'stop'
elif int(plr_guess) == 0:
break
else:
print 'Give value between ', str(const_min), ' to ' + str(const_max), ' please.'
if guess_game == 'stop':
end_game = 'stop'
os.system(clear_screen)
print 'You got it!'
print 'It took ', str(rounds), ' rounds to guess right number.'
print
print ' Press <y> + <ENTER> to play again. <ENTER> only to quit.'
if raw_input() == 'y':
guess_game = end_game = 'play'