Hangman iliti vjesala u Pythonu

Hangman iliti vjesala u Pythonu

offline
  • lnenad  Male
  • Stručni saradnik
    Web
  • Nenad
  • Arhitetak
  • Pridružio: 16 Jan 2007
  • Poruke: 2860
  • Gde živiš: Bijeljina

Malo mi dosadno na ovom raspustu pa sam prekjuce krenuo da ucim python tokom dana. Citao sam osnovne stvari na http://docs.python.org/tutorial/ i za prvi "projekat" sam odlucio da napravim vjesala. Profesionalna deformacija mi je da mislim na engleskom dok programiram pa sam sve komentare pisao na engleskom Smile

Mozda ce nekom koristiti a i meni je bilo lijepo napraviti nesto, za pocetak sam zadovoljan Ziveli . 173 linija koda.

#!/usr/bin/env python #A simple Python hangman v.0.1 from random import randint #import library for random number generation #Hangman generation function def printhang(ntries):     if tries == 1:         print """                   |         |         |         |         """     elif tries == 2:         print """           ___         |          |         |         |         """     elif tries == 3:         print """           ___         |  O         |         |         |         """     elif tries == 4:         print """           ___         |  O         |  |         |         |         """     elif tries == 5:         print """           ___         |  O         | \|/         |         |                 """     elif tries == 6:         print """           ___         |  O         | \|/         | / \         |                 """     elif tries == 7:         print """           ___         |  |         |  O         | \|/         | / \                 """ print """ Welcome to Hangmanpy   ___  |  O  | \|/  | / \   The original python text based hangman game By Nenad Lukic To begin, choose your game type 1 - Movies 2 - Actors 3 - Actresses 4 - Animals 5 - Cities """ gtype = int(raw_input("Enter game type (1,2,3,4,5) :")) if gtype == 1:     print """You have chosen Movies"""     gstring = ["Iron Man", "American Pie", "Titanic"] elif gtype == 2:     print """You have chosen Actors"""     gstring = ["Jim Carey"] elif gtype == 3:     print """You have chosen Actresses"""     gstring = ["Sandra Bullock", "Natalie Portman", "Jennifer Aniston"] elif gtype == 4:     print """You have chosen Animals"""     gstring = ["Giraffe", "Elephant", "Hippopotamus", "Wolf"] elif gtype == 5:     print """You have chosen Cities"""     gstring = ["Beijing", "London", "Belgrade", "Hong Kong", "Moscow"] else:     print "Invalid game type." randnum = randint(0,len(gstring)-1) #Get a random number chosen_string = gstring[randnum] #Choose a string tries = 0 #Number of tries taken by player maxtries = 7 #Maximum number of tries allowed lenc = len(chosen_string) #Length of the chosen string mlenc = 0 #Minus the spaces noguessed = 0 #Number of guessed letters showstr = "" #String to show the letters #Generate the string with underscores and spaces, while counting the number of spaces for x in range(0,lenc):     if chosen_string[x]== " ":         mlenc = mlenc+1         showstr = showstr+" "     else:         showstr = showstr+"_" lenw = lenc-mlenc #How many letters does the user need to guess triedl = [] #Array that will contain the letters the user tried to guess while tries<maxtries:     usedl = []     guessed = 0 #Variable that we'll use to check if the user succesfully guessed a letter     triedit = 0 #Variable that we'll use to see if the user used a letter which he used previously     gtry = raw_input("Input your letter: ")     for x in range (0, len(gtry)):         if triedl.count(gtry[x].lower()):             triedit = 1 #The user already used that letter(s)     if triedit != 1:         for x in range (0, len(gtry)):             triedl.append(gtry[x].lower()) #Get that letter into the triedl array         if len(gtry)>1:             for x in range(0,len(gtry)):                 for y in range(0,lenc):                     if gtry[x] != " ":                         if gtry[x].lower() == chosen_string[y].lower():                             if usedl.count(gtry[x].lower()):                                 pass                             else:                                 usedl.append(gtry[x].lower())                                 #print "Guessed " + gtry[x] used for debugging                                 guessed = 1;                                 noguessed = noguessed + 1                                 newshowstr = showstr[:y]+chosen_string[y]+showstr[y+1:]                                 showstr = newshowstr         else:             for x in range(0,lenc):                 if gtry.lower() == chosen_string[x].lower():                     if gtry != " ":                         #print "Guessed " + gtry used for debugging                         guessed = 1                         noguessed = noguessed + 1                         newshowstr = showstr[:x]+chosen_string[x]+showstr[x+1:]                         showstr = newshowstr         if guessed == 0:             tries = tries+1             print "You didn't guess anything this time. Number of tries: " + str(tries)             printhang(tries)         else:             print showstr         if noguessed >= lenw:             print "You have succesfully completed the hangman, you have made " + str(tries) + " errors"             break         if tries == maxtries:             print "Sorry but you failed, better luck next time"     else:         print "You have already tried that letter"

Sledeca stvar koju cu da dodam jeste citanje podataka iz direktorijuma i upisivanje istih u izbor tipa igre.

edit: Dodao 5 linija koda koje su sredile jedan nacin varanja Mr. Green Morao sam da ispravim Mr. Green



Registruj se da bi učestvovao u diskusiji. Registrovanim korisnicima se NE prikazuju reklame unutar poruka.
offline
  • daxytt  Male
  • Zaslužni građanin
  • Pridružio: 30 Okt 2012
  • Poruke: 623

exe stadalone verzija? cisto pitam, igra mi se



offline
  • Pridružio: 18 Jan 2012
  • Poruke: 477

Evo i ovdje hangman igrica, kod je malko drugaciji, ali isto moze posluziti: http://inventwithpython.com/chapter9.html
Ako nekoga zanima kako se pravi, objasnjen je svaki dio koda Smile

Ko je trenutno na forumu
 

Ukupno su 898 korisnika na forumu :: 20 registrovanih, 2 sakrivenih i 876 gosta   ::   [ Administrator ] [ Supermoderator ] [ Moderator ] :: Detaljnije

Najviše korisnika na forumu ikad bilo je 3466 - dana 01 Jun 2021 17:07

Korisnici koji su trenutno na forumu:
Korisnici trenutno na forumu: celik, crnitrn, Dorcolac, dragan_mig31, Džordžino, GandorCC, GeorgeAzadian, Georgius, jackreacher011011, Koridor, MegaVLAdaR, mile33, Parker, raptorsi, raso76, royst33, ruger357, stagezin, voja64, yufighter