Skip to main content

Hello, python

Jan 20, 2021Ntindle
Loading...

More Python Posts

radians to degrees

Nov 19, 2022CodeCatch

0 likes • 0 views

from math import pi
def rads_to_degrees(rad):
return (rad * 180.0) / pi
rads_to_degrees(pi / 2) # 90.0

Read Dataset from excel file

Oct 7, 2022KETRICK

0 likes • 0 views

import pandas as pd
x = pd.read_excel(FILE_NAME)
print(x)

Rock paper scissors

Nov 19, 2022CodeCatch

0 likes • 0 views

""" Rock Paper Scissors
----------------------------------------
"""
import random
import os
import re
os.system('cls' if os.name=='nt' else 'clear')
while (1 < 2):
print "\n"
print "Rock, Paper, Scissors - Shoot!"
userChoice = raw_input("Choose your weapon [R]ock], [P]aper, or [S]cissors: ")
if not re.match("[SsRrPp]", userChoice):
print "Please choose a letter:"
print "[R]ock, [S]cissors or [P]aper."
continue
// Echo the user's choice
print "You chose: " + userChoice
choices = ['R', 'P', 'S']
opponenetChoice = random.choice(choices)
print "I chose: " + opponenetChoice
if opponenetChoice == str.upper(userChoice):
print "Tie! "
#if opponenetChoice == str("R") and str.upper(userChoice) == "P"
elif opponenetChoice == 'R' and userChoice.upper() == 'S':
print "Scissors beats rock, I win! "
continue
elif opponenetChoice == 'S' and userChoice.upper() == 'P':
print "Scissors beats paper! I win! "
continue
elif opponenetChoice == 'P' and userChoice.upper() == 'R':
print "Paper beat rock, I win! "
continue
else:
print "You win!"

Bogo Sort

Nov 19, 2022CodeCatch

0 likes • 0 views

# Python program for implementation of Bogo Sort
import random
# Sorts array a[0..n-1] using Bogo sort
def bogoSort(a):
n = len(a)
while (is_sorted(a)== False):
shuffle(a)
# To check if array is sorted or not
def is_sorted(a):
n = len(a)
for i in range(0, n-1):
if (a[i] > a[i+1] ):
return False
return True
# To generate permuatation of the array
def shuffle(a):
n = len(a)
for i in range (0,n):
r = random.randint(0,n-1)
a[i], a[r] = a[r], a[i]
# Driver code to test above
a = [3, 2, 4, 1, 0, 5]
bogoSort(a)
print("Sorted array :")
for i in range(len(a)):
print ("%d" %a[i]),

Create a Pascal’s Triangle

May 31, 2023CodeCatch

0 likes • 0 views

def generate_pascals_triangle(num_rows):
triangle = []
for row in range(num_rows):
# Initialize the row with 1
current_row = [1]
# Calculate the values for the current row
if row > 0:
previous_row = triangle[row - 1]
for i in range(len(previous_row) - 1):
current_row.append(previous_row[i] + previous_row[i + 1])
# Append 1 at the end of the row
current_row.append(1)
# Add the current row to the triangle
triangle.append(current_row)
return triangle
def display_pascals_triangle(triangle):
for row in triangle:
for number in row:
print(number, end=" ")
print()
# Prompt the user for the number of rows
num_rows = int(input("Enter the number of rows for Pascal's Triangle: "))
# Generate Pascal's Triangle
pascals_triangle = generate_pascals_triangle(num_rows)
# Display Pascal's Triangle
display_pascals_triangle(pascals_triangle)

Connect to MYSQL and create a database

Nov 19, 2022CodeCatch

0 likes • 0 views

import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE mydatabase")