Record_python_final1
Record_python_final1
def calculateLength(n):
length = 0
while(n != 0):
length = length + 1
n = n//10
return length
num = int(input("Enter a number:"))
rem = sum = 0
len = calculateLength(num)
#Makes a copy of the original number num
n = num
#Calculates the sum of digits powered with their respective position
while(num> 0):
rem = num%10
sum = sum + int(rem**len)
num = num//10
len = len - 1
#Checks whether the sum is equal to the number itself
if(sum == n):
print(str(n) + " is a Disarium number")
else:
print(str(n) + " is not a Disarium number")
Result:
Result:
Result:
import random
user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
if user_action == computer_action:
print("Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
if computer_action == "scissors":
print("Rock smashes scissors! You win!")
else:
print("Paper covers rock! You lose.")
elif user_action == "paper":
if computer_action == "rock":
print("Paper covers rock! You win!")
else:
print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
if computer_action == "paper":
print("Scissors cuts paper! You win!")
else:
print("Rock smashes scissors! You lose.")
Result:
Result:
#Reading csv file with pandas and giving names to each column
index=["color","color_name","hex","R","G","B"];
csv = pd.read_csv('colors.csv', names=index, header=None);
#function to calculate minimum distance from all colors and get the most
matching color
def getColorName(R,G,B):
minimum = 10000;
for i in range(len(csv)):
d = abs(R- int(csv.loc[i,"R"])) + abs(G- int(csv.loc[i,"G"]))+ abs(B-
int(csv.loc[i,"B"]));
if(d<=minimum):
minimum = d;
cname = csv.loc[i,"color_name"]
return cname;
if (clicked):
#cv2.rectangle(image, startpoint, endpoint, color, thickness)-1 fills entire rectangle
cv2.rectangle(img,(20,20), (750,60), (b,g,r), -1);
#cv2.putText(img,text,start,font(0-7),fontScale,color,thickness,lineType )
cv2.putText(img, text,(50,50),2,0.8,(255,255,255),2,cv2.LINE_AA);
cv2.destroyAllWindows()
Result:
import json
from difflib import get_close_matches
# Loading data from json file
# in python dictionary
data =json.load(open("c://test/+” “dictionary.json"))
def translate(w):
# converts to lower case
w =w.lower()
if w in data:
return data[w]
# for getting close matches of word
elif len(get_close_matches(w, data.keys())) > 0:
yn =input("Did you mean % s instead? Enter Y if yes, or N if no:
"%get_close_matches(w, data.keys())[0])
yn =yn.lower()
if yn =="y":
return data[get_close_matches(w, data.keys())[0]]
elif yn =="n":
return "The word doesn't exist. Please double check it."
else:
return "We didn't understand your entry."
else:
return "The word doesn't exist. Please double check it."
# Driver code
word =input("Enter word: ")
output =translate(word)
if type(output) ==list:
for item in output:
print(item)
else:
print(output)
input('Press ENTER to exit')
Result:
import random
min = 1
max = 6
roll_again = "yes"
Result:
import random
hidden =random.randrange(1,201)
guess =int(input("Please enter your guess: "))
if guess == hidden:
print("Hit!")
elif guess < hidden:
print("Your guess is too low")
else:
print("Your guess is too high")
print(hidden)
Result:
import time
name = input("What is your name? ")
print ("Hello, " + name, "Time to play hangman!")
print (" ")
time.sleep(1)
print ("Start guessing...")
time.sleep(0.5)
word = "secret"
guesses = ''
turns = 10
while turns > 0:
failed = 0
for char in word:
if char in guesses:
print (char)
else:
print ("_",)
failed += 1
if failed == 0:
print ("You won" )
break
guess = input("guess a character:")
guesses += guess
if guess not in word:
turns -= 1
print ("Wrong")
print ("You have", + turns, 'more guesses' )
if turns == 0:
print ("You Lost" )
Result: