Student Community
Resources Join the most intelligent community of young coders. Browse, save & play with codes to understand how they work, and code your own ideas to earn badges & scholarships.

# Hangman Game

## posted 2 years ago

while loop
function
random
dictionary
if-else

Play the classic hangman game. Guess the correct word in the least number of tries and save the Hangman!

``````def get_player_name():
player_name = input('Enter your name: ')
return player_name

player_name = get_player_name()
print('Hello',player_name,',Lets Play Hangman!')

words_possible = {'secret':['_','_','c','_' ,'_','t'],'game':['_', '_', 'm', '_'],'dragon':['_','r','_','_','_','n'],'python':['_','_','t','_','o','_'],'interpreter':['_','_','_','e','_','p','_','_','t','_','r',]}

import random
word = random.choice(list(words_possible.keys()))
word_to_print = words_possible[word]
print(word_to_print)

chances=5
while chances>0:
guess = input('Enter a character: ')

if guess in word:
print('Correct Guess')
for i in range(len(word)):
if word[i] == guess:
word_to_print[i]=guess
print(word_to_print)
if '_' not in word_to_print:
print('You Won!!')
break
else:
chances = chances - 1
print('Wrong Guess')
print('Chances left: ',chances)

if chances == 0:
print('Sorry you lost!!')
print("   _____ \n"
"  |     | \n"
"  |     |\n"
"  |     | \n"
"  |     O \n"
"  |    /|\ \n"
"  |    / \ \n"
"__|__\n")
elif chances == 1:

print("  _____ \n"
"  |     | \n"
"  |     |\n"
"  |     | \n"
"  |     O \n"
"  |     \n"
"  |     \n"
"__|__\n")

elif chances == 2:

print("  _____ \n"
"  |     | \n"
"  |     |\n"
"  |     | \n"
"  |      \n"
"  |     \n"
"  |     \n"
"__|__\n")

elif chances == 3:

print("  _____ \n"
"  |     | \n"
"  |     |\n"
"  |      \n"
"  |      \n"
"  |     \n"
"  |     \n"
"__|__\n")

elif chances == 4:

print("  _____ \n"
"  |     | \n"
"  |     \n"
"  |      \n"
"  |      \n"
"  |     \n"
"  |     \n"
"__|__\n")``````

## posted 2 years ago

if-else
elif
mathematics

You can think of a number between 1 to 60 and then the interpreter will guess your number. Hope you like it.

``````x=0

A=1

B=2

C=4

D=8

E=16

F=32

print("Welcome to Mind Reader Magic Trick!")

print("Think of a number from 1 to 60.")

print("\n1,5,3,7,9,11,13,15,17,19,39,33,31,37,35,53,57,59,51,55,47,41,45,49,43,25,23,27,21,29")

while True:

print("Is the number in the list above?")

in_list=input("Y for Yes and N for No: ")

if in_list=="Y" or in_list=="y":

x=x+A

break

elif in_list=="N" or in_list=="n":

x=x

break

else:

print("\n2,7,6,3,10,18,15,11,14,19,30,26,27,22,23,31,42,51,34,43,54,35,46,55,38,47,58,50,39,59")

while True:

print("Is the number in the list above?")

in_list=input("Y for Yes and N for No: ")

if in_list=="Y" or in_list=="y":

x=x+B

break

elif in_list=="N" or in_list=="n":

x=x

break

else:

print("\n4,6,7,5,12,20,15,14,13,21,30,28,23,29,22,38,36,31,37,39,47,46,52,44,45,55,54,60,53")

while True:

print("Is the number in the list above?")

in_list=input("Y for Yes and N for No: ")

if in_list=="Y" or in_list=="y":

x=x+C

break

elif in_list=="N" or in_list=="n":

x=x

break

else:

print("\n8,13,26,31,44,57,9,14,27,40,45,58,10,15,28,41,46,59,11,24,29,42,47,60,12,25,30,43,56")

while True:

print("Is the number in the list above?")

in_list=input("Y for Yes and N for No: ")

if in_list=="Y" or in_list=="y":

x=x+D

break

elif in_list=="N" or in_list=="n":

x=x

break

else:

print("\n16,22,57,49,25,17,23,27,54,60,18,24,48,59,30,19,26,53,29,56,20,31,58,50,51,21,52,28,55")

while True:

print("Is the number in the list above?")

in_list=input("Y for Yes and N for No: ")

if in_list=="Y" or in_list=="y":

x=x+E

break

elif in_list=="N" or in_list=="n":

x=x

break

else:

print("\n32,38,42,46,33,39,47,49,59,34,40,44,51,58,35,43,48,57,56,36,45,50,55,54,37,41,60,52,53")

while True:

print("Is the number in the list above?")

in_list=input("Y for Yes and N for No: ")

if in_list=="Y" or in_list=="y":

x=x+F

break

elif in_list=="N" or in_list=="n":

x=x

break

else:

print("Voilà!!! The number you thought was",x,".")``````

# Currency Convertor

## posted 5 months ago

if-else

``````print ("Type 1 for USD")
print ("Type 2 for INR")
print ("Type 3 for EUR")
print ("Type 4 for GBP")
choice = (input ("Enter your choice: "))
print ("Conversion from", choice, "to?")
print ("Type 1 for USD")
print ("Type 2 for INR")
print ("Type 3 for EUR")
print ("Type 4 for GBP")
choice2 = (input ("Enter your choice: "))
amount = float(input("Amount?"))
if choice == "1" and choice2 == "2":
print (amount, "USD in INR is", (amount*79.00))
if choice == "1" and choice2 == "3":
print (amount, "USD in EUR is", (amount*0.99))
if choice == "1" and choice2 == "4":
print (amount, "USD in GBP is", (amount*0.83))
if choice == "2" and choice2 == "1":
print (amount, "INR in USD is", (amount*0.013))
if choice == "2" and choice2 == "3":
print (amount, "INR in EUR is", (amount*0.012))
if choice == "2" and choice2 == "4":
print (amount, "INR in GBP is", (amount*0.010))
if choice == "3" and choice2 == "1":
print (amount, "EUR in USD is", (amount*1.01))
if choice == "3" and choice2 == "2":
print (amount, "EUR in INR is", (amount*80.81))
if choice == "3" and choice2 == "4":
print (amount, "EUR in GBP is", (amount*0.84))
if choice == "4" and choice2 == "1":
print (amount, "GBP in INR is", (amount*96.01))
if choice == "4" and choice2 == "2":
print (amount, "GBP in USD is", (amount*1.20))
if choice == "4" and choice2 == "3":
print (amount, "GBP in EUR is", (amount*1.19))``````

# Break the Code

## posted a year ago

if-else

HI This code is by Chithranjan Its a game called Break the code Pls react if you liked it ❤🎉🔥

``````print("Hello there Programmers 🖐🖐")
print("Its Chithranjan")
name=input("Enter you name")
print(f"Hi {name} Today I got a game for you called Break the code")
print(" the rule is Each of us will try to guess each other's code, one chance for both of us to guess each other's code and the code should only be of 2 digits:")
print("alright lets start")
print("Try guessing mine first")

print("-------------------------Round 1-------------------------")
if try1 == '75':
print("Yes you are correct let me try yours")
else:
print("No its not right let me try yours")

print("If I am right type small 'y' and press enter if wrong type small 'n' and press enter")
_try_1=input("is it 99?")
if _try_1 == 'y':
print("HurraH")
else :
print("oh I am wrong")

print("So lets see who is the winner")
points1 = 0
points2 = 0
if try1 == '75':
points2 = points2+1

if _try_1 == 'y':
points1 = points1+1

if points1>points2:
print("I win, better luck next time")
if points1<points2:
print("You win congratulations")
if points1 == points2:
print("Its a draw")

# Web helper

## posted a year ago

webbrowser
if-else

This code will help you search in YouTube or google faster and easier. NOTE: THIS CODE ONLY WORKS ON PYTHON OR PYTHON IDE LIKE PYCHARM AND VISUAL STUDIO. YOU NEED TO ALSO INSTALL THE NEEDED PIP PACKAGES TO RUN THIS CODE. THE NEEDED COMMAND IS GIVEN ON THE CODE. please like my code toooo......

``````from googlesearch import * #pip install googlesearch

import pywhatkit #pip install pywhatkit

import webbrowser #pip install webbrowser

print("Hi I am Chithranjan in this code you can search in youtube or google faster and easier")

print("So what would you like to open google or youtube type in small letters")

print("What would you like to search in google")

query = input("WHat do you want me to search:")

chrome_path = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe %s'

for url in search(query, tld="co.in", num=1, stop = 1, pause = 2):

song=input("What do you want to play in youtube")

print("Playing" +song)

pywhatkit.playonyt(song)

print("Thank you for using this code")

print('I would really appreciate if you like this code')``````

# Play Penalty Kicker Game

## posted 2 years ago

games
function
for loop
elif

Welcome to Penalty Kicker. Game rules: First you will take penalties then the pc will(pc character name is James). Both sides will take 5 Penalties. You can choose three position in both taking Penalties and Goal Keeping, which are Middle, Left and Right. You have to type \'L\' for Left, \'M\' for Middle and \'R\' for Right. If you will type anything rather than L, M or R your chance will be taken as well as score will not increase. Best of Luck :-)

``````import random as rd

# declaring James positions
j_position = ['L', 'M', 'R']

# main game function
def game():
# taking player name

# declaring default scores
jamesScore = 0
playerScore = 0

print('\nPenalty Time!\nBest of luck :-)')

# penalty taking
for i in range(5):
# taking choice of james and player
jamesChoice = rd.choice(j_position)
playerChoice = input("\nType 'L' or 'l' for Left , 'M' or 'm' for Middle and 'R' or 'r' for Right : ")

# comparing the choices
# taking the case if james stopped the penalty
if playerChoice.upper() in j_position:
if playerChoice.upper() == jamesChoice:
print('\nJames stopped the penalty!\n')
# printing current scores
print(f'Current Scores: \nPlayer Score = {playerScore} \nJames Score = {jamesScore} ')

# if james didn't stopped then player scored
else:
print(f'\n{playerName} scored!\n')
playerScore = playerScore + 1
# printing current scores
print(f'Current Scores: \nPlayer Score = {playerScore} \nJames Score = {jamesScore} ')

# printing choices of james and player
print(f'\nPlayer Chose = {playerChoice.upper()}\nJames Chose = {jamesChoice}')

# if player didn't choose between L, M or R
else:
print('You can only choose L, M or R')

print('\nPenalty Saving Time!\nStop James before he destroy you ;-)')

# penalty saving
for j in range(5):
# taking choice of james and player
jamesChoice = rd.choice(j_position)
playerChoice = input("\nType 'L' or 'l' for Left , 'M' or 'm' for Middle and 'R' or 'r' for Right : ")

# comparing the choices
# taking the case if j stopped the penalty
if playerChoice.upper() in j_position:
if playerChoice.upper() == jamesChoice:
print(f'\n{playerName} stopped the penalty!\n')
# printing current scores
print(f'Current Scores: \nPlayer Score = {playerScore} \nJames Score = {jamesScore} ')

# if player didn't stopped then james scored
else:
print('\nJames scored!\n')
jamesScore = jamesScore + 1
# printing current scores
print(f'Current Scores: \nPlayer Score = {playerScore}\nJames Score = {jamesScore} ')

# printing choices of james and player
print(f'\nPlayer Chose = {playerChoice.upper()}\nJames Chose = {jamesChoice}')

# if player didn't choose between L, M or R
else:
print('You can only choose L, M or R')

# DECLARING THE WINNER
# if player won
if playerScore > jamesScore:
scoreDif = playerScore - jamesScore
print(f'\n{playerName} won!\n')

# a bit of commentry
if scoreDif >= 3:
print(f'{playerName} dominated James!')
elif scoreDif == 1:
print(f'That was a very close match but there can be only one winner and that is {playerName}')
else:
print('That was a wonderful match!')

# if james won
elif playerScore < jamesScore:
scoreDif = jamesScore - playerScore
print('\nJames won!\n')

# a bit of commentry
if scoreDif >= 3:
print(f'James dominated {playerName}!')
elif scoreDif == 1:
print(f'That was a very close match but there can be only one winner and that is James')
else:
print('That was a wonderful match!')

# if match tied
else:
print('The match is tied!\nWonderful play by both Players')

# asking the player if he/she wanna play again
again = input("Wanna play again? Press 'y'or 'Y' to play again or press 'x' or 'X' to exit : ")
if again.lower() == 'y':
game()
else:
print('\nThanks for playing. Hoping to see you soon')

# explaining the game rules to players

print(
'Welcome to Penalty Kicker!\n\nGame rules:\nFirst you will take penalties then the pc will(pc character name is James):\nBoth sides will take 5 Penalties\nYou can choose three position in both taking Penalties and Goal Keeping, That are Middle, Left and Right\nYou have to type \'L\' for Left, \'M\' for Middle and \'R\' for Right\nIf you will type anything rather than L, M or R your chance will be taken as well as score will not increase \nBest of Luck :-)\n')

# starting the game
start = input("Press 's'or 'S' to start game or press 'x' or 'X' to exit : ")

if start.lower() == 's':
game()
else:

# Tic Tac Toe or 'X' and 'O'

## posted 2 years ago

if-else
function
while loop
games

Tic-tac-toe, is a paper-and-pencil game for two players, X or x and O or o, who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in a diagonal, horizontal, or vertical row is the winner. It is a solved game with a forced draw assuming best play from both players.

``````import random

player_1 = input("Enter player 1 name : ")

player_2 = input("Enter player 2 name : ")

characterChoose = random.randint(1,2)

game = True

winner = "none"

count = 0

#assign characters "x or o"

if characterChoose == 1:

print(f"{player_1} will choose first")

player1_character = input(f"{player_1} what character do you want : (x or o)")

if player1_character == "x":

player2_character = "o"

else:

player2_character = "x"

player1_character = "o"

else:

print(f"{player_2} will choose first")

player2_character = input(f"{player_2} what character do you want : (x or o)")

if player2_character == "x":

player1_character = "o"

else:

player1_character = "x"

player2_character = "o"

print(f"{player_1} = {player1_character}\n{player_2} = {player2_character}\n")

#who will go first

whoWillGoFirst = random.randint(1,2)

if whoWillGoFirst == 1:

print(f"{player_1} will go first\n{player_2} will go second")

else:

print(f"{player_2} will go first\n{player_1} will go second")

board = [" "," "," "," "," "," "," "," "," "," "]

#making the board

def board_game():

print('   |   |')

print(' ' + board + ' | ' + board + ' | ' + board)

print('   |   |')

print('-----------')

print('   |   |')

print(' ' + board + ' | ' + board + ' | ' + board)

print('   |   |')

print('-----------')

print('   |   |')

print(' ' + board + ' | ' + board + ' | ' + board)

print('   |   |')

# making functions for player 1 turn and player 2

def player1_turn():

position = int(input(f"{player_1} which position do you want to place your character (1 to 9)"))

position = position - 1

if board[position] == " ":

board.pop(position)

board.insert(position,player1_character)

print("the board has been updated")

board_game()

else:

print("the slot has been taken")

player1_turn()

def player2_turn():

position = int(input(f"{player_2} which position do you want to place your character (1 to 9)"))

position = position - 1

if board[position] == " ":

board.pop(position)

board.insert(position,player2_character)

print("the board has been updated")

board_game()

else:

print("the slot has been taken")

player2_turn()

#making function that will check winner

def winnerCheck():

global game

global winner

global count

if (board == "x" and board == "x" and board == "x"

or

board == "x" and board == "x" and board == "x"

or

board == "x" and board == "x" and board == "x"

or

board == "x" and board == "x" and board == "x"

or

board == "x" and board == "x" and board == "x"

or

board == "x" and board == "x" and board == "x"

or

board == "x" and board == "x" and board == "x"

or

board == "x" and board == "x" and board == "x"):

if player1_character == "x":

print(f"\n{player_1} wins the match")

game = False

else:

print(f"\n{player_2} wins the match")

game = False

elif (board == "o" and board == "o" and board == "o"

or

board == "o" and board == "o" and board == "o"

or

board == "o" and board == "o" and board == "o"

or

board == "o" and board == "o" and board == "o"

or

board == "o" and board == "o" and board == "o"

or

board == "o" and board == "o" and board == "o"

or

board == "o" and board == "o" and board == "o"

or

board == "o" and board == "o" and board == "o"):

if player1_character == "o":

print(f"\n{player_1} wins the match")

game = False

else:

print(f"\n{player_2} wins the match")

game = False

else:

if count == 9:

print("tie")

game = False

return game

#main game

while game == True:

if whoWillGoFirst == 1:

player1_turn()

count = count + 1

winner_check = winnerCheck()

if winner_check == False:

break

player2_turn()

count = count + 1

winner_check = winnerCheck()

if winner_check == False:

break

else:

player2_turn()

count = count + 1

winner_check = winnerCheck()

if winner_check == False:

break

player1_turn()

count = count + 1

winner_check = winnerCheck()

if winner_check == False:

break``````

# Jairaj Spacex

## posted a year ago

if-else

Know your cost of traveling to planets like mars and venus with Jairaj Spacex

``````import decimal

def num2words(num):
num = decimal.Decimal(num)
decimal_part = num - int(num)
num = int(num)

if decimal_part:
return num2words(num) + " point " + (" ".join(num2words(i) for i in str(decimal_part)[2:]))

under_20 = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen']
tens = ['Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety']
above_100 = {100: 'Hundred', 1000: 'Thousand', 100000: 'Lakhs', 10000000: 'Crores'}

if num < 20:
return str(num)

if num < 100:
return str(num)

# find the appropriate pivot - 'Million' in 3,603,550, or 'Thousand' in 603,550
pivot = max([key for key in above_100.keys() if key <= num])

return num2words(num // pivot) + ' ' + above_100[pivot] + ('' if num % pivot==0 else ' ' + num2words(num % pivot))

print("""
█░█░█ █▀▀ █░░ █▀▀ █▀█ █▀▄▀█ █▀▀   ▀█▀ █▀█
▀▄▀▄▀ ██▄ █▄▄ █▄▄ █▄█ █░▀░█ ██▄   ░█░ █▄█

░░█ ▄▀█ █ █▀█ ▄▀█ ░░█   █▀ █▀█ ▄▀█ █▀▀ █▀▀ ▀▄▀
█▄█ █▀█ █ █▀▄ █▀█ █▄█   ▄█ █▀▀ █▀█ █▄▄ ██▄ █░█
""")
print("\nWelcome",name,"to Jairaj SpaceX.\n")
rockets = ["fuel x", "fuel y"]
rocket=input(("\nWe have two types of rockets \n1. Fuel x \n2. Fuel y \n(choose 1 or 2): "))
print("Okay",name,"you will be travelling using",rocket,".\n")

print("Where do you want to travel in space?")
planetChoice=input("You can travel to \n1. Moon \n2. Mars \n3. Mercury \n4. Venus \n5. Jupiter \n \n(choose 1,2,3,4 or 5): ")
planets = [
{ "name": "Moon", "distance": 384400 },
{ "name": "Mars", "distance": 227900000 },
{ "name": "Mercury", "distance": 57910000 },
{ "name": "Venus", "distance": 108200000 },
{ "name": "Jupiter", "distance": 778500000 }
]

for i in range(len(planets)):
planet = planets[i]
if int(planetChoice) - 1 == i:
print(planet["name"],"is",num2words(planet["distance"]),"kms far!")
fuel_x_cost = 10000
fuel_y_cost = 5000
price = fuel_x_cost * planet["distance"]
if int(rocket) == 2:
price = fuel_y_cost * planet["distance"]
print("It will cost you ₹",num2words(price),"to travel!")``````

# Hypo-Leg-Sqrt

## posted a year ago

mathematics
if-else

This is a code by KDBeditz_Official© and Chitranjan Now you can find hypotenuse, Leg and square root of any number

``````choice = input("Enter 1 for hypotenuse, enter 2 for length of the leg and 3 for square root. ENTER YOUR CHOICE : ")
if choice == '1':
num1=int(input("Enter value of leg1 : "))
num2=int(input("Enter value of leg2 : "))
a = num1*num1
b = num2*num2
numa = a+b
num_sqrt = numa ** 0.5
print("The hypotenuse is", num_sqrt)

if choice == '2':
print("NOTE 🛑 The length of hypotenuse is always greater than the leg")
num3=int(input("Enter value of leg: "))
num4=int(input("Enter value of hypotenuse: "))
c = num3*num3
d = num4*num4
numb = d-c
num_sqrt = numb ** 0.5
print("the length of the other leg is",num_sqrt)

if choice == '3':
num = float(input('Enter a number: '))
num_sqrt = num ** 0.5
print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))``````

## posted a year ago

games
function
elif

100 squares full of traps and tricks…Roll the dice and try your luck! Ladders will take you up but Snakes will take you down!

``````import random

Max = 100
player1squarenumber = 0
player2squarenumber = 0

print("""Welcome to Snake and Ladder Game.

Rules:
1. Initally both the players are at starting position i.e. 0.
Take it in turns to roll the dice.
Move forward the number of spaces shown on the dice.
2. If you lands at the bottom of a ladder, you can move up to the top of the ladder.
3. If you lands on the head of a snake, you must slide down to the bottom of the snake.
4. The first player to get to the FINAL position is the winner.
5. Type R to roll the dice
6. Type Q to quit.
7. Have fun!""")

player_turn_text = [
"Go.",
"Lets win this.",
"",
]

snakeBite = [
"woops",
"ow",
"snake bite",
"oh no",
"sorry"
]

"woohoo",
"woww",
"nailed it",
"oh good ...",
"yaayyy"
]

snakes = {
8: 5,
18: 1,
26: 8,
39: 7,
51: 2,
54: 23,
56: 1,
60: 20,
75: 30,
83: 40,
85: 59,
90: 48,
92: 25,
97: 87,
99: 5
}

2: 20,
6: 19,
11: 28,
15: 39,
17: 78,
22: 37,
38: 50,
49: 61,
57: 77,
61: 76,
73: 87,
81: 97,
88: 98
}

print("\n")
print("\n")

def getDiceValue():
dicevalue = random.randint(1, 6)
print("It is a " + str(dicevalue))
return dicevalue

def GotSnakeBite(old_value, current_value, player_name):
print("\n" + random.choice(snakeBite).upper() + " ~~~~~~~~>")
print("\n" + player_name + " you got a snake bite. You are down from " + str(old_value) + " to " + str(current_value))

print("\n" + random.choice(ladder_jump).upper() + " ########")
print("\n" + player_name + " you climbed the ladder from " + str(old_value) + " to " + str(current_value))

def GameStart():
player1squarenumber = 0
player2squarenumber = 0

player1name = input("Please enter a name for first player: ")
player2name = input("Please enter a name for second player: ")
print("\n")
print("Game will be played between '" + player1name + "' and '" + player2name + "'.")
print("\n")

while True:
input1 = input(player1name + ": " + random.choice(player_turn_text) + " Press R to roll the dice: ")
if input1 == "Q":
break
print("Rolling dice...")
diceValue = getDiceValue()
if player1squarenumber + diceValue > Max:
print("You need " + str(Max - player1squarenumber) + " to win.")
else:
player1squarenumber = player1squarenumber + diceValue

if player1squarenumber == Max:
print("Congratulations! " + player1name + " won the game.")
print("\n")
break
old_value = player1squarenumber

elif player1squarenumber in snakes:
old_value = player1squarenumber
player1squarenumber = snakes[player1squarenumber]
GotSnakeBite(old_value, player1squarenumber, player1name)

print(player1name + " current number: " + str(player1squarenumber))
print("\n")

input2 = input(player2name + ": " + random.choice(player_turn_text) + " Press R to roll the dice: ")
if input2 == "Q":
break
print("Rolling dice...")
diceValue = getDiceValue()

if player2squarenumber + diceValue > Max:
print("You need " + str(Max - player2squarenumber) + " to win.")
else:
player2squarenumber = player2squarenumber + diceValue

if player2squarenumber == Max:
print("Congratulations! " + player2name + " won the game.")
break