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 : 1
What decimal number would you like to stop at : 10

Press Enter to see all values from 1 to 10 ...

Decimal ----- Binary ----- Hexadecimal 
---------------------------------------
1-----1-----1
2-----10-----2
3-----11-----3
4-----100-----4
5-----101-----5
6-----110-----6
7-----111-----7
8-----1000-----8
9-----1001-----9
10-----1010-----a

Program to print the factors of a given number. 

p = True
while 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 : 30

Factors of 30 are : 
1
2
3
5
6
10
15
30
Do you want to run again? (Yes/No)Yes
Enter number : 25

Factors of 25 are : 
1
5
25
Do you want to run again? (Yes/No)No

Program of a game to guess a number.

import random

print('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 :
        break

if 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 : 12
Choose a larger number
Take a guess : 20
Choose a larger number
Take a guess : 23
Choose a larger number
Take a guess : 25
Choose a larger number
Take a guess : 28
Choose a smaller number
Gameover!!
>>> 
Choose a number between 1 to 30 in your mind ..... You will get 5 chances ..... 
Take a guess : 28
Choose a smaller number
Take a guess : 25
Choose a smaller number
Take a guess : 12
Choose a smaller number
Take a guess : 10
Yayy!! You guessed the number in 3 guesses.

Program of rock, paper and scissor game.

import random

n = int(input('How many rounds you would like to play?'))
chances = ['rock','paper','scissor']
player = 0
computer = 0
winner = None

for 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+1
        print('Player wins the round!')
    elif winner == 'computer' :
        computer = computer+1
        print('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?3

Round 1 : 
Player : 0
Computer : 0
Rock, paper or scissor?rock
Computer picks rock
Player picks rock
Its a tie!!
The round was tie!

Round 1 : 
Player : 0
Computer : 0
Rock, paper or scissor?paper
Computer picks rock
Player picks paper
Paper covers rock!!
Player wins the round!

Round 1 : 
Player : 1
Computer : 0
Rock, paper or scissor?rock
Computer picks rock
Player picks rock
Its a tie!!
The round was tie!

Final Results -
Total number of rounds : 3
Player score : 1
Computer score : 0
YOU WINS THE GAME..
>>> 
How many rounds you would like to play?5

Round 1 : 
Player : 0
Computer : 0
Rock, paper or scissor?rock
Computer picks scissor
Player picks rock
Rock breaks scissor!!
Player wins the round!

Round 1 : 
Player : 1
Computer : 0
Rock, paper or scissor?paper
Computer picks rock
Player picks paper
Paper covers rock!!
Player wins the round!

Round 1 : 
Player : 2
Computer : 0
Rock, paper or scissor?scissor
Computer picks paper
Player picks scissor
Scissor cuts paper!!
Player wins the round!

Round 1 : 
Player : 3
Computer : 0
Rock, paper or scissor?paper
Computer picks scissor
Player picks paper
Scissor cuts paper!!
Computer wins the round!

Round 1 : 
Player : 3
Computer : 1
Rock, paper or scissor?rock
Computer picks rock
Player picks rock
Its a tie!!
The round was tie!

Final Results -
Total number of rounds : 5
Player score : 3
Computer score : 1
YOU WINS THE GAME..

            
        
        





 

Comments

Post a Comment

Popular posts from this blog

Some tips about the right way to charge your phone

Software Basics

Some unique and weird things about Japan