Computing N choose R using Python

Hi! This program calculates the number of ways possible to choose r items out of n items. I am still working on the program and am exploring more efficient ways to compute it because the computer cannot handle multiplication and division to a really high extent. I hope you like it!

n= int(input(("How many items are available to choose out of? "))) r= int(input("How many items do you want to choose? ")) # nCr = (N!)/ r!(n-r)! nfacbyrfac = 1 for x in range(r, n+1, 1): nfacbyrfac= nfacbyrfac*x nminusrfactorial= 1 for z in range(1,(n-r)+1,1): nminusrfactorial= nminusrfactorial* z nCr= (nfacbyrfac)/ (nminusrfactorial) print (nCr)

Function compilation

#In this program I have compiled some functions and let the user choose which one #they want to use. The aproach that I took when solving this problem is instead of #defining the functions first made it so that only the chosen function is defined # and the others are defined once the user chooses them. #Prithvi #In this program instead of defining all of the functions and then giving the commands, I programmed it so each function is only defined after I choose it while True: #Here I made it so that all of the functions would be put inside a loop so if 1 ended it would ask you the question again The_Question = input("what function would you like me to run?") if The_Question == "prime": def prime(): number = int(input("Enter The Number")) # In my primecomposite program I made it so that it was in an infinite loop so that once it ended t would break into the infinite loop again if number > 1: for i in range(2,int(number/2)): if (number % i == 0): print(number, "is not a Prime Number") break else: print(number,"is a Prime number") #I added the question to stop and try another function to make the program easier to make it more user friendly VB = input(" would you like to continue?") if VB == "yes": prime() else: return prime() if The_Question == "calendar": import calendar while 8==8: # by saying "while 8==8" I am creating an infinite loop #The print_month function is only defined b = (input("would you like to print out the calendar for a month or a year month/year ")) def print_month(): c = int(input("What year would you like the month to be from. ex. 2020 ")) d = int(input("What month would you like to be printed 1 for january, 2 for febuary etc.. ")) print (calendar.month(c,d)) VB2 = input("would you like to continue") if VB2 == "yes": print_month() else: return def print_year(): e = int(input("Which year would you like the calendar be printed of 2015, 2019 etc ")) for i in range(1,13): print (calendar.month (e,i)) if b == "month": print_month() if b == "year": print_year() if The_Question == "miles and kilometers": f = input("Would you like to convert from kiliometer to mile or from mile to kilometer mtk/ktm? ") if f == "mtk": miles_question = int(input("How many miles would you like to convert? ")) if f == "ktm": kilometer_question = int(input("How many miles would you like to convert? ")) def mtk(): res1 = miles_question * 1.608 print(res1,"kilometers") def ktm(): res2 = kilometer_question * 0.621 print(res2,"miles") if f == "mtk": mtk() if f == "ktm": ktm() g = input("would you like to make another conversion? ") if g == "yes": 0+0 if g == "no": break # If the user answers with multiplication that he/she would then need to say if they want a table or just want to multiply 2 numbers. if The_Question =="multiplication": MYlist = [] h = (input("would you like to show the multiplication table or would you like to multiply two numbers? table/two_numbers")) if h == "table": j = int(input("how many multiplication tables would you like answer 1-10")) for i in range (0, j): k = int(input("what multiplication tables do you want 1,2,3 ect.")) MYlist.append(k) # Once the user is done inputing all of the data, the program appends it all into a list which is needed in the table function def multtable(): for x in MYlist: for y in range (1,11): result = x * y print(x,"x", y, "=", result) multtable() if h == "two_numbers": def two_numbermultiply(): val3 = val1 * val2 print(val1, " x ", val2," = ", val3) val1 = int(input("input a number to be multiplied")) val2 = int(input("input a second number")) two_numbermultiply()

Function compilation

#In this program I have compiled some functions and let the user choose which one #they want to use. The aproach that I took when solving this problem is instead of #defining the functions first made it so that only the chosen function is defined # and the others are defined once the user chooses them. #Prithvi #In this program instead of defining all of the functions and then giving the commands, I programmed it so each function is only defined after I choose it while True: #Here I made it so that all of the functions would be put inside a loop so if 1 ended it would ask you the question again The_Question = input("what function would you like me to run?") if The_Question == "prime": def prime(): number = int(input("Enter The Number")) # In my primecomposite program I made it so that it was in an infinite loop so that once it ended t would break into the infinite loop again if number > 1: for i in range(2,int(number/2)): if (number % i == 0): print(number, "is not a Prime Number") break else: print(number,"is a Prime number") #I added the question to stop and try another function to make the program easier to make it more user friendly VB = input(" would you like to continue?") if VB == "yes": prime() else: return prime() if The_Question == "calendar": import calendar while 8==8: # by saying "while 8==8" I am creating an infinite loop #The print_month function is only defined b = (input("would you like to print out the calendar for a month or a year month/year ")) def print_month(): c = int(input("What year would you like the month to be from. ex. 2020 ")) d = int(input("What month would you like to be printed 1 for january, 2 for febuary etc.. ")) print (calendar.month(c,d)) VB2 = input("would you like to continue") if VB2 == "yes": print_month() else: return def print_year(): e = int(input("Which year would you like the calendar be printed of 2015, 2019 etc ")) for i in range(1,13): print (calendar.month (e,i)) if b == "month": print_month() if b == "year": print_year() if The_Question == "miles and kilometers": f = input("Would you like to convert from kiliometer to mile or from mile to kilometer mtk/ktm? ") if f == "mtk": miles_question = int(input("How many miles would you like to convert? ")) if f == "ktm": kilometer_question = int(input("How many miles would you like to convert? ")) def mtk(): res1 = miles_question * 1.608 print(res1,"kilometers") def ktm(): res2 = kilometer_question * 0.621 print(res2,"miles") if f == "mtk": mtk() if f == "ktm": ktm() g = input("would you like to make another conversion? ") if g == "yes": 0+0 if g == "no": break # If the user answers with multiplication that he/she would then need to say if they want a table or just want to multiply 2 numbers. if The_Question =="multiplication": MYlist = [] h = (input("would you like to show the multiplication table or would you like to multiply two numbers? table/two_numbers")) if h == "table": j = int(input("how many multiplication tables would you like answer 1-10")) for i in range (0, j): k = int(input("what multiplication tables do you want 1,2,3 ect.")) MYlist.append(k) # Once the user is done inputing all of the data, the program appends it all into a list which is needed in the table function def multtable(): for x in MYlist: for y in range (1,11): result = x * y print(x,"x", y, "=", result) multtable() if h == "two_numbers": def two_numbermultiply(): val3 = val1 * val2 print(val1, " x ", val2," = ", val3) val1 = int(input("input a number to be multiplied")) val2 = int(input("input a second number")) two_numbermultiply()

Python version of Blackjack

Hi! This is my attempt at making a Python version of the card game Blackjack. The program is at a rudimentary stage and is a work in progress. I am still adding to it since it lacks some of the nuances of Blackjack. I hope you enjoy it!

from random import randint # A list with the different cards gamecards= [2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King", "Ace"] # Defining different lists for the dealers's cards and the player's cards dealercards=[] playercards=[] # The dealer automatically get two cards in the beginning of the game while len(dealercards)< 2: x= randint(0,12) cardchosen= gamecards[x] if cardchosen == "Jack" or cardchosen =="Queen" or cardchosen == "King": dealercards.append(10) elif cardchosen== "Ace": dealercards.append(1) else: dealercards.append(cardchosen) print ("The dealer has a down facing card and", dealercards[1]) # The player automatically gets two cards in the beginning of the game while len(playercards)< 2: x= randint(0,12) playercardchosen= gamecards[x] if playercardchosen == "Jack" or playercardchosen=="Queen" or playercardchosen == "King": playercards.append(10) elif playercardchosen == "Ace": playercards.append(1) else: playercards.append(playercardchosen) if len(playercards) == 2: print ("Your cards are", playercards[0], "and", playercardchosen) Flag= False #Gameplay for the player while sum(playercards) < 21 and Flag == False: player_action= input("Do you want to hit or stay? ") if player_action == "stay" or player_action =="Stay": Flag= True elif player_action == "hit" or player_action =="Hit": y= randint(0,12) playercardchosen= gamecards[y] if playercardchosen == "Jack" or playercardchosen == "Queen" or playercardchosen == "King": playercards.append(10) print ("You got a", playercardchosen, "and your total now is", sum(playercards)) elif playercardchosen == "Ace": playercards.append(1) print ("You got a", playercardchosen, "and your total now is", sum(playercards)) else: playercards.append(playercardchosen) print ("You got a", playercardchosen, "and your total now is", sum(playercards)) #Finding what the dealer's total is while sum(dealercards)< 17: x= randint(0,12) cardchosen= gamecards[x] if cardchosen == "Jack" or cardchosen == "Queen" or cardchosen == "King": dealercards.append(10) print("The dealer's first card was", dealercards[0]) print ("The dealer got a", cardchosen, "and his total now is", sum(dealercards)) elif cardchosen == "Ace": dealercards.append(1) print (cardchosen) else :dealercards.append(cardchosen) print ("Your total is", sum(playercards), "and your cards are", playercards) print ("The dealer has ", dealercards, "and the total is", sum(dealercards)) if sum(dealercards) > 21 and sum(playercards) > 21: print ("It is a tie!") elif sum(dealercards) < 21 and sum(dealercards) > sum(playercards): print("The dealer wins!") elif sum(playercards) < 21 and sum (playercards) > sum(dealercards): print ("You win!") elif sum(playercards) > 21: print ("The dealer wins") elif sum(dealercards) > 21: print ("You win") elif sum(dealercards) == sum(playercards): print ("It is a tie!")

My Take On The DNA to Codon caculator

This is my take on the DNA converter. I Use a direct approach by writing out all the codons instead of using functions.

#i import randint to make a randomized DNa code from random import randint #i then make variebles to make typing easier A = "A" T = "T" C = "C" G = "G" i = 0 #i make two lists for Rna and Dna SEQ = [A, T, C, G] nSEQ = [] #ask the user to input numbers length = int(input("Enter a number ")) x = len(SEQ)-1 #below this randomizes the letters for i in range(0, length): y = SEQ[randint(0, x)] nSEQ.append(y) # i = i + 1 output = [] #and this is converting to RNA by appending the Rna code to the Dna code for j in range (0, len(nSEQ)): if nSEQ[j] == A: output.append("U") elif nSEQ[j] == T: output.append("A") elif nSEQ[j] == G: output.append("C") else: output.append("G") # i am printing this out so i can find where i went wrong print(nSEQ) print(output) amino ="" #i use the join function to group every three numbers for k in range(0,len(output),3): base3list=output[k:k+3] base3str="".join(base3list) print(base3str) codon = base3str #then i right every single codon and its trio along if (codon == "GCA" or codon == "GCC" or codon == "GCC" or codon == "GCU"): amino = "A" if (codon == "UGC" or codon == "UGU"): amino = "C" if (codon == "GAC" or codon == "GAU"): amino = "D" if (codon == "GAA" or codon == "GAG"): amino = "E" if (codon == "UUC" or codon == "UUU"): amino = "F" if (codon == "GGA" or codon == "GGC" or codon == "GGG" or codon == "GGU"): amino = "G" if (codon == "CAC" or codon == "CAU"): amino = "H" if (codon == "AUA" or codon == "AUC" or codon == "AUU"): amino = "I" if (codon == "AAA" or codon == "AAG"): amino = "K" if (codon == "UUA" or codon == "UUG" or codon == "CUC" or codon == "CUA" or codon == "CUG" or codon == "CUU"): amino = "L" if (codon == "AUG"): amino = "M" if (codon == "AAC" or codon == "AAU"): amino = "N" if (codon == "CCA" or codon == "CCC" or codon == "CCG" or codon == "CCU"): amino = "P" if (codon == "CAG" or codon == "CAA"): amino = "Q" if (codon == "AGA" or codon == "AGG" or codon == "CGA" or codon == "CGC" or codon == "CGG" or codon == "CGU"): amino = "R" if (codon == "AGC" or codon == "AGU" or codon == "UCA" or codon == "UCC" or codon == "UCG" or codon == "UCU"): amino = "S" if (codon == "ACA" or codon == "ACC" or codon == "ACG" or codon == "ACU"): amino = "T" if (codon == "GUA" or codon == "GUC" or codon == "GUG" or codon == "GGU"): amino = "V" if (codon == "UGG" ): amino = "W" if (codon == "UAC" or codon == "UAU" ): amino = "Y"

Castle drawn with turtle

This was something I made with turtle.Turtle(). I linked the reference image below. This took me quite a bit of time as I was still getting used to turtle and it is quite tedious


import turtle Frank = turtle.Turtle() #firsttower Frank.penup() Frank.goto(-87,-120) Frank.pendown() Frank.fillcolor('black') Frank.begin_fill() Frank.right(180) Frank.forward(130) Frank.right(90) Frank.forward(330) Frank.left(55) Frank.forward(55) Frank.right(55) Frank.forward(70) Frank.right(90) Frank.forward(30) Frank.right(90) Frank.forward(35) Frank.left(90) Frank.forward(30) Frank.left(90) Frank.forward(35) Frank.right(90) Frank.forward(30) Frank.right(90) Frank.forward(35) Frank.left(90) Frank.forward(30) Frank.left(90) Frank.forward(35) Frank.right(90) Frank.forward(30) Frank.right(90) Frank.forward(75) Frank.right(55) Frank.forward(55) Frank.left(55) Frank.forward(160) Frank.left(90) Frank.forward(240) #tower2 Frank.left(90) Frank.forward(160) Frank.left(55) Frank.forward(55) Frank.right(55) Frank.forward(70) Frank.right(90) Frank.forward(30) Frank.right(90) Frank.forward(35) Frank.left(90) Frank.forward(30) Frank.left(90) Frank.forward(35) Frank.right(90) Frank.forward(30) Frank.right(90) Frank.forward(35) Frank.left(90) Frank.forward(30) Frank.left(90) Frank.forward(35) Frank.right(90) Frank.forward(30) Frank.right(90) Frank.forward(75) Frank.right(55) Frank.forward(55) Frank.left(55) Frank.forward(320) Frank.right(90) Frank.forward(130) Frank.right(90) Frank.forward(65) Frank.circle(50,180) Frank.forward(65) Frank.end_fill() Frank.penup() Frank.goto(-170,47) Frank.pendown() #window 1 Frank.fillcolor('white') Frank.begin_fill() Frank.right(180) Frank.forward(50) Frank.circle(17,180) Frank.forward(50) Frank.left(90) Frank.forward(31) Frank.end_fill() Frank.penup() Frank.goto(130,47) Frank.pendown() #window2 Frank.fillcolor('white') Frank.begin_fill() Frank.left(90) Frank.forward(50) Frank.circle(17,180) Frank.forward(50) Frank.left(90) Frank.forward(31) Frank.end_fill() Frank.penup() Frank.goto(-120,30) Frank.pendown() #stubrectanglethingy Frank.fillcolor('black') Frank.begin_fill() Frank.right(270) Frank.forward(45) Frank.right(90) Frank.forward(55) Frank.right(90) Frank.forward(35) Frank.penup() Frank.goto(-10,30) Frank.pendown() Frank.right(180) Frank.forward(45) Frank.right(90) Frank.forward(55) Frank.right(90) Frank.forward(35) Frank.end_fill() Frank.penup() Frank.goto(50,0)

Hangman Game by Anika

This is a hangman game I made. It took me around 3 classes to make. It involved several lists and variables. I want to make it so it draws out the character like a regular game of hangman, but the code would be a bit more tedious. I hope you like it !!

print("HANGMAN") from random import randint valid1 = 0 x = 0 hg = 0 valid2 = 0 #valid letters validlet = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] #valid number of guesses validguess = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25"] #valid minimum lengths validmin = ["3", "4", "5", "6", "7", "8", "9", "10"] #list of words that will be randomly picked by the random allwords = [ "dog", "cat", "van", "person", "incomprehensible", "avocado", "celebrity", "pandemic", "pencilcase", "pillowcase", "penny", "orangutan", "caramel", "tomorrow", "garbage", "astronaut", "apartment", "walking", "leaf", "jump", "digestive", "poster"] #length of list above y = len(allwords) validwords = [] while valid1 == 0: attempts = input("How many incorrect attempts do you want? [1-25] ") #if the input is not one of the numbers from the number of guesses list, print not a valid number if attempts not in validguess: print("Not a valid number from 1 to 25") else: valid1 = 1 attempts = int(attempts) #if the input is not equa; to a valid minum length, print invalif while valid2 == 0: minlen = input("What's the minimum length of the word? [3-10] ") if minlen not in validmin: print("Not a valid number from 3 to 10") else: valid2 = 1 minlen = int(minlen) while x in range(-1, y - 1): x = x + 1 #lenx is equal to the length of elements i allwords lenx = int(len(allwords[x])) valid3 = max(lenx, minlen) if valid3 == lenx: validwords.append(allwords[x]) #generation of word wordnm = randint(0, len(validwords) - 1) word = validwords[wordnm] wordlen = len(word) #'list' of elemnts in the guess guess = [] wordlist = [] #guessing code (all the code relating to the user guessing is from here onwards) for x in range(0, wordlen): x = x + 1 guess.append("*") wordlist.append(word[x - 1]) #user guesses (what it asks the user, where they guess, and text for errors) while attempts > 0: print(guess) guess1 = input("Enter your guess: ") #guess isnt a valid letter within the list, say it is not a valid guess if guess1 not in validlet: print( "This is not a valid guess. Try inputting a lowercase English letter" ) #wrong guess (so what happens when they guess a letter wrong) else: if guess1 not in wordlist: if attempts != 1: print("This letter is not in the word. Try again") #user loses else: print( "This letter is not in the word. You have ran out of attempts. The word was " + word) attempts = attempts - 1 #user is right (code when user guesses the correct letter) else: print("This letter is in the word") while hg in range(0, len(wordlist)): if wordlist[hg] == guess1: guess[hg] = guess1 hg = hg + 1 #user wins the game if guess == wordlist: print("You won! Congrats. The word is " + word) while True: aohuni = 1 hg = 0 #made by anika choudhary

OpenPyXl in PyCharm

Hello! I have made a program which synchronizes Microsoft Excel and Apple Numbers through a package in Python called openpyxl. This program can correct wrong numbers in excel or apple numbers. It also creates a bar graph. This can only work in PyCharm, not repl.it.

import openpyxl as xl from openpyxl.chart import BarChart, Reference def processs_workbook(filename): wb = xl.load_workbook('transactions.xlsx') sheet = wb['Sheet1'] for row in range(2, sheet.max_row + 1): cell = sheet.cell(row, 3) corrected_price = cell.value * 0.9 corrected_price_cell = sheet.cell(row, 4) corrected_price_cell.value = corrected_price values = Reference(sheet, min_row=2, max_row=sheet.max_row, min_col=4, max_col=4) chart = BarChart() chart.add_data(values) sheet.add_chart(chart, 'e2') wb.save(filename)

Emoji Converter

Who knew you could but actual emojis in python! I put three of the basic ones: Smiley face, sad face, and blank face. For a smiley face, write any sentence on the terminal with a :), for a sad face, so the same thing except with :(, for a blank face, do something like this :| Have a Nice day! I hope you Enjoy 😁!

#Emoji Converter Program def emoji_converter(message): #The Main Function words = message.split(" ") emojis = { #Line 4 to line 8 is our main emoji converter. Use :) for a smiley face, :( for a sad face, and :| for a expressionless face. ":)":"😀", ":(":"🙁", ":|":"😑" } output = "" #Line 9 to 12 is for our output for word in words: output += emojis.get(word, word) + " " return output message = input(">") #line 15 to 17 is the end result of our program result = emoji_converter(message) print(result)

Calculator GUI using PYQT5

A gui that is very similar to the calculator app.

Prime/Composite Number Program

This is just a simple prime/composite number program for how to print prime and composite numbers

primelist = ("2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97") def primenumber(num): ''' while True: answer = str(input("Would you like to print a list of all prime numbers from 1 to 100 or enter a number to tell whether it is prime or composite? All/Single ")) if answer == "single" : ''' #num = int(input("Enter the number you want: ")) #add new if statement if num == 2: print("it is a prime number") else: print("Let's find out if that number is prime or not.") composite = 0 for i in range(2,num): if (num % i) == 0: composite = composite + 1 print(composite) if composite == 0: print(num, "is a prime number") else: print(num,"is a composite number") #print(num, "is a prime number") #print(i,"times",num//i,"is",num) #print(num, "is a prime number") ''' elif answer == "prime 100": print(primelist) ''' start = str(input("Let's Start! Enter to continue!")) while True: ask = input("Would you like to continue? yes or no?: ") if ask == "yes": answer = str(input("Would you like to print a list of all prime numbers from 1 to 100 or enter a number to tell whether it is prime or composite? prime 100/single? prime 100 will print a list of numbers from 1 to 100. Single will ask you for what number do you wan't to be called as prime or composite. ")) if answer == "single" : num = int(input("Enter the number you want: ")) primenumber(num) elif answer == "prime 100": print(primelist) if ask == "no": break
Aarush Dhawan Jul 27

It has some things that I will fix and update soon. I will post the updated program later and explain the program though comments.

Fibonacci Sequence

#numbers n0=0 n1=1 #amount of fibonacci numbers input=input("How many fibonacci numbers would you like") #logic for i in range(1, input+1): print(n0) temp = n0 n0 =n1 n1 = n1 + temp;
1 2 ... 29