Skip to content

Instantly share code, notes, and snippets.

View srahuliitb's full-sized avatar
🏠
Working from home

Rahul Singh srahuliitb

🏠
Working from home
View GitHub Profile
@srahuliitb
srahuliitb / guess_a_number.py
Created September 9, 2017 18:44
Generate a random number between 1 and 9 (including 1 and 9). Ask the user to guess the number, then tell them whether they guessed too low, too high, or exactly right.
import random
rand_num = random.randint(1, 9)
user_guess = int(raw_input("Guess a number between 1 to 9: "))
attempts = 0
while user_guess != rand_num:
@srahuliitb
srahuliitb / rock_paper_scissor.py
Last active February 9, 2023 14:06
Make a two-player Rock-Paper-Scissors game. (Hint: Ask for player plays (using input), compare them, print out a message of congratulations to the winner, and ask if the players want to start a new game)
p1_call = raw_input("Player 1, Enter rock/paper/scissors: ")
p2_call = raw_input("Player 2, Enter rock/paper/scissors: ")
def rock_paper_scissors(player_1, player_2):
a_list = ["rock", "paper", "scissors"]
while player_1 not in a_list or player_2 not in a_list:
print "Invalid input. Please give a valid input."
player_1 = raw_input("Player 1, Enter rock/paper/scissors: ")
player_2 = raw_input("Player 2, Enter rock/paper/scissors: ")
@srahuliitb
srahuliitb / list_comprehension.py
Created September 6, 2017 18:21
Let’s say I give you a list saved in a variable: a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]. Write one line of Python that takes this list a and makes a new list that has only the even elements of this list in it.
a = [x ** 2 for x in range(1, 11)]
a_even = []
for i in a:
if i % 2 == 0:
a_even.append(i)
print a_even
@srahuliitb
srahuliitb / palindrome.py
Created September 6, 2017 12:45
Ask the user for a string and print out whether this string is a palindrome or not. (A palindrome is a string that reads the same forwards and backwards.)
string = raw_input("Enter a string: ")
reverse_string = string[::-1]
if string == reverse_string:
print "The given string is a palindrome."
else:
print "The given string is NOT a palindrome."
@srahuliitb
srahuliitb / 100_years_old.py
Created September 6, 2017 12:04
Create a program that asks the user to enter their name and their age. Print out a message addressed to them that tells them the year that they will turn 100 years old.
name = raw_input("Enter your name: ")
age = int(raw_input("Enter your age in numerals: "))
hundred_years = 2017 + 100 - age
print "Hello %s! You will turn 100 years old in the year %s." %(name, hundred_years)
@srahuliitb
srahuliitb / list_overlap.py
Created September 6, 2017 11:40
A program that returns a list that contains only the elements that are common between two lists without duplicates.
# Prgram which returns list of common elements in two lists.
list_a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
list_b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
# Removing duplicates.
def remove_duplicates(any_list):
new_list = []
for item in any_list:
if item not in new_list:
@srahuliitb
srahuliitb / list_less_than_5.py
Created August 29, 2017 04:17
Take a list, say for example this one: a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] and write a program that prints out all the elements of the list that are less than 5.
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = []
for i in a:
if i < 5:
b.append(i)
print b
@srahuliitb
srahuliitb / odd_even
Created August 29, 2017 03:56
Ask the user for a number. Depending on whether the number is even or odd, print out an appropriate message to the user.
number = int(raw_input("Enter a number: "))
if number % 2 == 0:
print (str(number) + " is an even number.")
else:
print (str(number) + " is an odd number.")