0% found this document useful (0 votes)
48 views2 pages

AI Slip 4

Uploaded by

gauravwani532
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views2 pages

AI Slip 4

Uploaded by

gauravwani532
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Q.1)Write a program to implement Hangman game using python.

Description: Hangman is a classic word-guessing game. The user should guess the word correctly by
entering alphabets of the user choice. The Program will get input as single alphabet from the user
and it will matchmaking with the alphabets in the original.

import time #importing the time module


name = input("What is your name? ") #welcoming the user
print ("Hello, " + name, "Time to play hangman!")
print ("Start guessing...")
time.sleep(0.5)
word = ("secret") #here we set the secret. You can select any
word to play with.
guesses = '' #creates an variable with an empty value
turns = 10 #determine the number of turns
while turns > 0: # Create a while loop #check if the turns are
more than zero
failed = 0
for char in word: # for every character in secret_word
if char in guesses: # see if the character is in the players
guess
print (char,end=""), # print then out the character
else: # if not found, print a dash
print ("_",end=""), # and increase the failed counter
with one
failed += 1 # if failed is equal to zero
if failed == 0: # print You Won
print ("You won")
break # exit the script
guess = input("guess a character:") # ask the user go guess a
character
guesses += guess # set the players guess to guesses
if guess not in word: # if the guess is not found in the secret
word
turns -= 1 # turns counter decreases with 1 (now 9
print ("Wrong") # print wrong
print ("You have", + turns, 'more guesses' ) # how many turns
are left
if turns == 0: # if the turns are equal to zero
print ("You Lose" )# print "You Lose"

Q.2) Write a Python program to implement Breadth First Search algorithm. Refer the following graph
as an Input for the program.[Initial node=1,Goal node=8]

graph = {
'A' : ['B','C','G'],
'B' : ['D', 'E'],
'C' : ['F'],
'G' : [],
'D' : [],
'E' : ['F'],
'F' : []
}
visited = [] # List to keep track of visited nodes.
queue = [] #Initialize a queue
def bfs(visited,graph,node):
visited.append(node)
queue.append(node)
while queue:
s = queue.pop(0)
print (s, end = " ")
for i in graph[s]:#checking adjacent
if i not in visited:
visited.append(i)
queue.append(i)
bfs(visited, graph, 'A')

You might also like