Posts

Showing posts from December, 2020

Python Games

Image
Snake Game #Snake Tutorial Python import math import random import pygame import tkinter as tk from tkinter import messagebox class cube(object):     rows = 20     w = 500     def __init__(self,start,dirnx=1,dirny=0,color=(255,0,0)):         self.pos = start         self.dirnx = 1         self.dirny = 0         self.color = color              def move(self, dirnx, dirny):         self.dirnx = dirnx         self.dirny = dirny         self.pos = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny)     def draw(self, surface, eyes=False):         dis = self.w // self.rows         i = self.pos[0]         j = self.pos[1]         pygame.draw.rect(surface, self.color, (i*dis+1,j*dis+1, dis-2, dis-2)) ...

Pattern python

Image
Photo by  Pixabay  from  Pexels  ##PATTERN 1 rows=int(input("Enter number of rows:")) #each_number will be 1 in first iteration and then 2 and so on for each_number in range(1,rows+1): #printing number will be in range (1 , i+1) i.e  (1,2) but 2 is excluded so only 1 will be printed , likewise the rest for printing_pattern in range(1,each_number+1): print(printing_pattern,end=" ") #The print() function inserts a new line at the end, by default In Python 3, end =" " appends space instead of newline. print() OUTPUT: Enter number of rows:5 1  1 2  1 2 3  1 2 3 4  1 2 3 4 5  ##PATTERN 2 rows=int(input("Enter number of rows:")) for each_number in range(1,rows+1): for printing_pattern in range(1,each_number+1): print(each_number,end="") print() OUTPUT: Enter number of rows:5 1 22 333 4444 55555 ##PATTERN 3 # rows=int(input("Enter number of rows:")) for each_number in range(r...

Python code part 2

 #WAP to find whether a given username contains less than 10 characters or not username = input("Enter username:") if  len(username) < 10: print("\nusername is valid") else: print("\nusername is not valid") # WAP to take input in list and find largest number in list array_size=int(input("Enter array_size:")) array=[] for numbers in range(array_size): values=input("Enter array elements:") array.append(values) print(array) largest=array[0] for next_number in array: if largest<next_number: largest=next_number print("The largest in the array : ",largest) #we check next_number2 in array then check condition if next_number2 is less than smallest then the next_number2 becomes the smallest which is done in the next step smallest=array[0] for next_number2 in array: if smallest > next_number2: smallest=next_number2 print("The smallest in the array:",smallest) OUTPUT: Enter array_size:4 Enter array el...

Python Codes from pc

Image
Image source: Negative Space name=input("Enter name:") color=input("Enter fav color:") print(name,"likes",color) ____________________________________________________________________________ weight=int(input("Enter weight:")) option=input("\nIn pound or kg?\np:Pound\nk:kilogram\nEnter option:") if(option.lower()=="p"):     weight_in_kg=0.4535*weight     print("Your weight_in_kg:",weight_in_kg) elif(option.lower()=="k"):     weight_in_pound=2.2046*weight     print("Your weight_in_pound:",weight_in_pound) ____________________________________________________________________________ print("Price of house : 1 million dollar") good_credit=False  if good_credit:     discount=10000000*0.1     price=10000000-discount     print("Discounted price:",price) else:     discount=10000000*0.2     price=10000000-discount     print(price) ___________________________________________________________...