Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.41 KB | None | 0 0
  1. import sys
  2. import random
  3. import timeit
  4.  
  5. countries = []
  6. capitals = []
  7. not_in_word = []
  8. let = ""
  9. lifes = 5
  10. hint_count = 0
  11. start_time = timeit.default_timer() #Starting of timer
  12. number_try = 0
  13.  
  14. # This below is responsible for the
  15. # txt file handling. It takes out
  16. # the list of countries and capitols
  17. # separates them, and then appends to different lists
  18. with open("countries_and_capitals.txt", "r") as f:
  19.     for line in f:
  20.         line = line.strip()
  21.         cap = line.split(' | ')
  22.         capitals.append(cap[1])
  23.         countries.append(cap[0])
  24.  
  25.  
  26. #Choosing a random capital
  27. capital = random.choice(capitals)
  28. index_capital = capitals.index(capital)
  29. index_country = index_capital
  30.  
  31. #Replace name's letter of capital to dashes
  32. dashes = ""
  33. for i in capital:
  34.     dashes += "_"
  35.  
  36.  
  37.  
  38. # Converting capital and dashes
  39. # variables into lists
  40. letter_list = list(capital.upper())
  41. dashes_list = list(dashes)
  42.  
  43. #print("\n" + capital + "\n")
  44. print("Welcome to hangman.exe.\n\nHuman.\n\nI wonder if you can guess\n\nthe name of the capital.")
  45.  
  46. print("\n\n" + " ".join(dashes_list) + "\n")
  47.  
  48. # Main while loop. Responsible for the
  49. # whole game mechanic.
  50. while dashes_list != letter_list:
  51.  
  52.  
  53.     guessing = input("\nDo u want to guess the whole name? (y/n) ")
  54.  
  55.     # First if function that user encounters,
  56.     # starts with guessing variable
  57.     # checks if u want to enter a whole word
  58.     if guessing == "y":
  59.         number_try = number_try + 1
  60.         print("\nOh I see.\nMr Smart.")
  61.         word = input("\nEnter the capital name: ")
  62.         word = word.lower()
  63.  
  64.         # Checking if the word is correct
  65.         if word == capital.lower():
  66.             stop_time = timeit.default_timer() #End of time
  67.             print("\n" + capital)
  68.             print("\nCongratulations.\nYou're a genius.")
  69.             print("You guessed after " + str(number_try) + " tries. It took you: %.1f" %(stop_time - start_time) , "seconds")
  70.             break
  71.  
  72.         else:
  73.             lifes = lifes - 1
  74.             print("\nUps.\nI guess you're not that smart.\nMinus one life.\nLifes left: " + str(lifes) + "\n")
  75.  
  76.  
  77.     # If u dont want to enter a whole word
  78.     # code jumps to this place, where
  79.     # letter choosing happens
  80.     elif guessing == "n":
  81.         number_try = number_try + 1
  82.         let = input("\nEnter a letter: ")
  83.         let = let.upper()
  84.  
  85.         # Checking if a letter was already entered
  86.         if let in dashes_list:
  87.             print("\nOh human.\nYou've already used that letter.")
  88.             print("\nMinus 1 life.")
  89.             lifes = lifes - 1
  90.             print("Lifes left: " + str(lifes) + "\n")
  91.            
  92.            
  93.  
  94.         # Here the variable let is being checked
  95.         # if the hidden word consist the variable
  96.         # a for loop happens, that loop
  97.         # changes each dash of hidden word
  98.         # to a letter which was guessed by the user
  99.         if let in letter_list:
  100.             for i in range(len(letter_list)):
  101.                 if let == letter_list[i]:
  102.                     dashes_list[i] = letter_list[i]
  103.             print("\n" + " ".join(dashes_list))
  104.  
  105.  
  106.         elif len(let) > 1:
  107.             lifes = lifes - 1
  108.             print("\nNo cheating.\nCheater.\n-1 life for that.")
  109.             print("Lifes left: " + str(lifes) + "\n")
  110.  
  111.         else:
  112.             lifes = lifes - 1
  113.             not_in_word.append(let)
  114.             print("\n" + " ".join(dashes_list))
  115.             print("\nWrong letter. Try again.\nLifes left: " + str(lifes) + "\n")
  116.  
  117.  
  118.  
  119.     # Here user can get a hint
  120.     # the hint is shown only when conditions are met
  121.     if lifes == 2 and hint_count == 0:
  122.         print("\nWell, well, well.\nPetty human.")
  123.         print("\nDoesn't even know his own continent")
  124.         hint = input("\nDo you need help\nHuman?\n(y/n) ")
  125.         if hint == "y":
  126.             hint_count = hint_count + 1
  127.             print("\nShame.\nAnd they say that humans are intelligent.\n")
  128.             print("You're looking for the capital of " + countries[index_country])
  129.         elif hint == "n":
  130.             print("\nBrave human.\nYou wont last.")
  131.  
  132.  
  133.     elif lifes == 0:
  134.         print("You lost.\nLearn some geography.\nFor christ sake.")
  135.         break
  136.    
  137.    
  138.  
  139. stop_time = timeit.default_timer() #End of time
  140. print("\nYou win this round human.")print("You guessed after " + str(number_try) + " tries. It took you: %.1f" %(stop_time - start_time) , "seconds")
  141. sys.exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement