Some Python Projects
Some Python Projects
Program to print decimal, binary and hexadecimal numbers between the entered decimal numbers.
a = int(input('What decimal number would you like to start at : '))b = int(input('What decimal number would you like to stop at : '))print('\nPress Enter to see all values from ' + str(a) + ' to ' + str(b)+ ' ...\n')print('Decimal ----- Binary ----- Hexadecimal \n---------------------------------------')for i in range(a,b+1):print(str(i) + ' ----- ' + str(bin(i)[2:]) + ' ----- ' + str(hex(i)[2:]))
Output :-
What decimal number would you like to start at : 1What decimal number would you like to stop at : 10Press Enter to see all values from 1 to 10 ...Decimal ----- Binary ----- Hexadecimal---------------------------------------1-----1-----12-----10-----23-----11-----34-----100-----45-----101-----56-----110-----67-----111-----78-----1000-----89-----1001-----910-----1010-----a
Program to print the factors of a given number.
p = Truewhile p :num = int(input('Enter number : '))factors = []for i in range(1,num+1) :if num%i == 0 :factors.append(i)print('\nFactors of ' + str(num) + ' are : ')for i in factors :print(i)ch = input('Do you want to run again? (Yes/No)')if ch != 'Yes' :p = False
Output :-
Enter number : 30Factors of 30 are :12356101530Do you want to run again? (Yes/No)YesEnter number : 25Factors of 25 are :1525Do you want to run again? (Yes/No)No
Program of a game to guess a number.
import randomprint('Choose a number between 1 to 30 in your mind ..... You will get 5 chances ......')num = random.randint(1,30)for i in range(0,5) :guess = int(input('Take a guess : '))if guess > num :print('Choose a smaller number')elif guess < num :print('Choose a larger number')else :breakif guess == num :print('Yayy!! You guessed the number in ' + str(i) + ' guesses.')else :print('Gameover!!')
Output :-
Choose a number between 1 to 30 in your mind ..... You will get 5 chances .....Take a guess : 12Choose a larger numberTake a guess : 20Choose a larger numberTake a guess : 23Choose a larger numberTake a guess : 25Choose a larger numberTake a guess : 28Choose a smaller numberGameover!!>>>Choose a number between 1 to 30 in your mind ..... You will get 5 chances .....Take a guess : 28Choose a smaller numberTake a guess : 25Choose a smaller numberTake a guess : 12Choose a smaller numberTake a guess : 10Yayy!! You guessed the number in 3 guesses.
Program of rock, paper and scissor game.
import randomn = int(input('How many rounds you would like to play?'))chances = ['rock','paper','scissor']player = 0computer = 0winner = Nonefor i in range(0,n) :print('\nRound 1 : ')print('Player : ' + str(player))print('Computer : ' + str(computer))ch = random.randint(0,2)comp_ch = chances[ch]play_ch = input('Rock, paper or scissor?').lower()if play_ch in chances :print('Computer picks ' + comp_ch)print ('Player picks ' + play_ch)if play_ch == 'rock' and comp_ch == 'rock' :winner = 'tie'print('Its a tie!!')elif play_ch == 'rock' and comp_ch == 'scissor' :winner = 'player'print('Rock breaks scissor!!')elif play_ch == 'rock' and comp_ch == 'paper' :winner = 'computer'print('Paper covers rock!!')elif play_ch == 'paper' and comp_ch == 'paper' :winner = 'tie'print('Its a tie!!')elif play_ch == 'paper' and comp_ch == 'scissor' :winner = 'computer'print('Scissor cuts paper!!')elif play_ch == 'paper' and comp_ch == 'rock' :winner = 'player'print('Paper covers rock!!')elif play_ch == 'scissor' and comp_ch == 'scissor' :winner = 'tie'print('Its a tie!!')elif play_ch == 'scissor' and comp_ch == 'paper' :winner = 'player'print('Scissor cuts paper!!')elif play_ch == 'scissor' and comp_ch == 'rock' :winner = 'computer'print('Rock breaks scissor!!')else :print('Please enter a valid choice!! Computer gets a point.')winner = 'computer'if winner == 'player' :player = player+1print('Player wins the round!')elif winner == 'computer' :computer = computer+1print('Computer wins the round!')else :print('The round was tie!')print('\nFinal Results -')print('Total number of rounds : ' + str(n))print('Player score : ' + str(player))print('Computer score : ' + str(computer))if player>computer :print('\tYOU WINS THE GAME..')elif computer<player :print('\tCOMPUTER WINS THE GAME..')else :print('\tTHE GAME WAS A TIE..')
Output :-
How many rounds you would like to play?3Round 1 :Player : 0Computer : 0Rock, paper or scissor?rockComputer picks rockPlayer picks rockIts a tie!!The round was tie!Round 1 :Player : 0Computer : 0Rock, paper or scissor?paperComputer picks rockPlayer picks paperPaper covers rock!!Player wins the round!Round 1 :Player : 1Computer : 0Rock, paper or scissor?rockComputer picks rockPlayer picks rockIts a tie!!The round was tie!Final Results -Total number of rounds : 3Player score : 1Computer score : 0YOU WINS THE GAME..>>>How many rounds you would like to play?5Round 1 :Player : 0Computer : 0Rock, paper or scissor?rockComputer picks scissorPlayer picks rockRock breaks scissor!!Player wins the round!Round 1 :Player : 1Computer : 0Rock, paper or scissor?paperComputer picks rockPlayer picks paperPaper covers rock!!Player wins the round!Round 1 :Player : 2Computer : 0Rock, paper or scissor?scissorComputer picks paperPlayer picks scissorScissor cuts paper!!Player wins the round!Round 1 :Player : 3Computer : 0Rock, paper or scissor?paperComputer picks scissorPlayer picks paperScissor cuts paper!!Computer wins the round!Round 1 :Player : 3Computer : 1Rock, paper or scissor?rockComputer picks rockPlayer picks rockIts a tie!!The round was tie!Final Results -Total number of rounds : 5Player score : 3Computer score : 1YOU WINS THE GAME..
It was fun😍💫
ReplyDelete