|
teknix
πβπ
’ππ
π°π‘ πΌπ⨻



Registered: 09/16/08
Posts: 11,953
|
Re: Learning How to program with Python. [Re: teknix]
#18861505 - 09/19/13 02:48 AM (10 years, 5 months ago) |
|
|
I had to fix the age count, I evidently saved the broken one :x
I'll tty guys later got class in a few hours and need some sleep.
|
teknix
πβπ
’ππ
π°π‘ πΌπ⨻



Registered: 09/16/08
Posts: 11,953
|
Re: Learning How to program with Python. [Re: lessismore]
#18861515 - 09/19/13 02:56 AM (10 years, 5 months ago) |
|
|
Quote:
Now find the bug(s) :-D , it has a bug if you enter a string instead of decimal hehe
Lol, I know, that's our next topic, I'm sure if you entered a letter it would bug too.
(I have to use a while or for and is.digit after I identify it as a separate variable, I just have too figure out the syntax and watnot. Or I could load re, but were not supposed to do it that way yet.)
|
teknix
πβπ
’ππ
π°π‘ πΌπ⨻



Registered: 09/16/08
Posts: 11,953
|
Re: Learning How to program with Python. [Re: teknix]
#18929669 - 10/04/13 04:41 AM (10 years, 4 months ago) |
|
|
""" Description: This program is designed to calculate amount of occurences of a given digit in a given number.
"""
#First I will print what the program does.
print("This program will compute the number of times a digit occurs in a number.") print()
#Second I will ask for the user input the first number.
largeNumberStr = input("Please enter the number as an integer for occurences to be counted: ")
#Third I will make sure the user input an applicable number.
while largeNumberStr.isdigit() == False: print("Please only enter positive integers.") largeNumberStr = input("Enter an integer: ")
#Fourth I will identify the user input as an integer.
largeNumberInt = int(largeNumberStr)
#Fifth I will ask for the user to input the second number.
singleDigitStr = input("Please enter a single digit you would like to be counted from above: ")
#Sixth I will make sure the user input a valid number.
while singleDigitStr.isdigit() == False: print("Please only enter integers") singleDigitStr = input("Please enter a single digit from 0-9: ")
#Seventh I will identify the second number as an integer.
singleDigitInt = int(singleDigitStr)
#Eighth I will verify that the integer is between 0 and 9.
while singleDigitInt > 9 or singleDigitStr.isdigit() == False : print("Please only enter integers") singleDigitStr = input("Please enter a single digit from 0-9: ") singleDigitInt = int(singleDigitStr) #Ninth I will identify the counters.
current = largeNumberInt count = 0 remainder = 0
#Tenth I will create a loop to reduce the large number input and add the #remainders to the counter if it is equal to the second input.
while current > 0: remainder=current%10 current=current//10 if remainder == singleDigitInt: count = count+1
#Eleventh I will print the results and quit the program.
print("The total amount of times",singleDigitInt,"occured was",count,".")
quit=input("Press enter to exit.")
Edited by teknix (10/11/13 11:59 AM)
|
teknix
πβπ
’ππ
π°π‘ πΌπ⨻



Registered: 09/16/08
Posts: 11,953
|
Re: Learning How to program with Python. [Re: teknix]
#18929673 - 10/04/13 04:45 AM (10 years, 4 months ago) |
|
|
another way to do the while instead of: while singleDigitStr.isdigit() == False:
is
while not singleDigitStr.isdigit():
Anyway, the above code is basically how to count in python, using remainders and quotients. the % is remainder and the // is quotient.
Edited by teknix (10/04/13 05:09 AM)
|
Penelope_Tree
Shamanic Panic



Registered: 07/31/09
Posts: 8,535
Loc: magic sugarcastle
|
Re: Learning How to program with Python. [Re: teknix]
#18936394 - 10/05/13 02:41 PM (10 years, 4 months ago) |
|
|
I'm learning Python for a free, online programming class (Bioinformatics - I'm really excited about it and hope I can keep up with it and a full-time job) I'm embarrassed to say that I'm having difficulty getting it running. I keep searching for answers online, but all I get are long, manual-type documents. Would somebody be willing to help me out here?
Thanks for making this thread, teknix. I'm going to go back through it when I figure out how to get Python working & try to follow along with the exercises you provided.
--------------------
full blown human
|
teknix
πβπ
’ππ
π°π‘ πΌπ⨻



Registered: 09/16/08
Posts: 11,953
|
Re: Learning How to program with Python. [Re: Penelope_Tree]
#18944095 - 10/07/13 10:34 AM (10 years, 4 months ago) |
|
|
This next assignment is to make a game similar to rock, paper, scissors but with memory and it is called Cowboy, Ninja, Bear.
The first thing I will do is import the random number generator.
import random random.seed()
#Second I will print what the program does and the options available to the user.
print(" ___ _ _ ___ ") print("/ _) | \| | | - ) ") print(" (_ _ | \ | _ | _ \ _") print("\___)(_)|_\__|(_)|___/(_) ") print() print("Welcome to Cowboy, Ninja, Bear!") print() print("The Ninja beats the Cowboy using lighting speed ninja kicks.") print() print("The Cowboy beats the Bear with his quick draw and perfect accuracy.") print() print("The Bear beats the Ninja with a strong swipe of his clawed paw.") print("") print("Type N to choose the Ninja.") print("") print("Type C to choose the Cowboy.") print("") print("Type B to choose the Bear.") print("") print("Type Q to quit the program.") print() #Third I will create the counters I will use for the program including an AI.
roundCounter = 0 tieCounter = 0
#Win counters
computerWin = 0 userWin = 0
#User choice counters, beginning at one to prevent multiplication by zero.
userCowboy = 1 userNinja = 1 userBear = 1
#Stomper counters, used to prevent the user from figuring out what the program, #will pick an keep picking the opposite.
ninjaStomper = 0 cowboyStomper = 0 bearStomper = 0
#Fourth I will assign the computer a random number between from one to three.
while True: computer = random.randint(1,3) #Fifth I will ask for user input within a loop. user = input("Please enter your choice, N for Ninja, C for Cowboy, B for Bear. ")
#Sixth I will convert any capitalized letters to lowercase.
user = user.lower() #Seventh I will convert user input into an integer. if user == "n": user = 1 elif user == "c": user = 2 elif user == "b": user = 3
elif user=="q": break #Eighth I will make sure input is valid. else: print("Invalid input, Enter N for Ninja, C for Cowboy, or B for Bear. ")
#Ninth I will add in an AI and use counters to determine computers choice. if userBear > userCowboy: computer = 2
if userCowboy > userNinja: computer = 1
if userNinja > userBear: computer = 3
#Tenth I will use the stompers if they keep picking the same choice.
if ninjaStomper > 2: computer = 3 if ninjaStomper > 7: ninjaStomper = 1 bearStomper = 0 cowboyStomper = 0 if ninjaStomper < 0: ninjaStomper = 1 if cowboyStomper > 2: computer = 1 if cowboyStomper > 7: cowboyStomper = 1 ninjaStomper = 0 bearStomper = 0 if cowboyStomper < 0: cowboyStomper = 1
if bearStomper > 2: computer = 2 if bearStomper > 7: bearStomper = 1 cowboyStomper = 0 ninjaStomper = 0 if bearStomper < 0: bearStomper = 1
#Eleventh I will print the results accordingly and keep track of counters. if user == computer: tieCounter = tieCounter +1 roundCounter = roundCounter +1 print ("The game ended in a tie. You chose the same as the computer.") print ("No one won round:",roundCounter,"with",userWin,"WINS and",computerWin,"LOSSES and",tieCounter,"ties.") print("-------------------------------------------------------------------") print()
elif user==1 and computer==2: roundCounter = roundCounter +1 userWin = userWin +1 userNinja = userNinja +1 ninjaStomper = ninjaStomper +1 cowboyStomper = cowboyStomper -1 print ("The Ninja beat the Cowboy using lighting speed ninja kicks.") print ("You won round:",roundCounter,"with",userWin,"WINS and",computerWin,"LOSSES and",tieCounter,"ties.") print("-------------------------------------------------------------------") print() elif user==1 and computer==3: roundCounter = roundCounter +1 computerWin = computerWin +1 userNinja = userNinja +1 ninjaStomper = ninjaStomper +1 bearStomper = bearStomper -1 print ("The Bear beat the Ninja with a strong swipe of his clawed paw.") print ("You lost round:",roundCounter,"with",userWin,"WINS and",computerWin,"LOSSES and",tieCounter,"ties.") print("-------------------------------------------------------------------") print()
elif user==2 and computer==1: roundCounter = roundCounter +1 computerWin = computerWin +1 userCowboy = userCowboy +1 ninjaStomper = ninjaStomper -1 cowboyStomper = cowboyStomper +1 bearStomper = bearStomper -1 print ("The Ninja beat the Cowboy using lighting speed ninja kicks.") print ("You lost round:",roundCounter,"with",userWin,"WINS and",computerWin,"LOSSES and",tieCounter,"ties.") print("-------------------------------------------------------------------") print() elif user==2 and computer==3: roundCounter = roundCounter +1 userWin = userWin +1 userCowboy = userCowboy +1 cowboyStomper = cowboyStomper +1 bearStomper = bearStomper -1 print ("The Cowboy beat the Bear with his quick draw and perfect accuracy.") print ("You won round:",roundCounter,"with",userWin,"WINS and",computerWin,"LOSSES and",tieCounter,"ties.") print("-------------------------------------------------------------------") print() elif user==3 and computer==2: roundCounter = roundCounter +1 computerWin = computerWin +1 userBear = userBear +1 cowboyStomper = cowboyStomper -1 bearStomper = bearStomper +1 print ("The Cowboy beat the Bear with his quick draw and perfect accuracy.") print ("You lost round:",roundCounter,"with",userWin,"WINS and",computerWin,"LOSSES and",tieCounter,"ties.") print("-------------------------------------------------------------------") print()
elif user==3 and computer==1: roundCounter = roundCounter +1 userWin = userWin +1 userBear = userBear +1 ninjaStomper = ninjaStomper -1 bearStomper = bearStomper +1 print ("The Bear beat the Ninja with a strong swipe of his clawed paw.") print ("You won round:",roundCounter,"with",userWin,"WINS and",computerWin,"LOSSES and",tieCounter,"ties.") print("-------------------------------------------------------------------") print()
#Finally I will print that the user chose to exit.
print("------------------------------------------------------------------") print ("Thank you for playing Cowboy, Ninja, Bear!") print ("The final score was",userWin,"wins and",computerWin,"losses over",roundCounter,"rounds.") print ("The Cowboy was selected by you",userCowboy -1,"times.") print ("The Ninja was selected by you",userNinja -1,"times.") print ("The Bear was selected by you",userBear -1,"times.") print ("Please play again.") print (" _______ _____ ____ ____ __ ___ ____") print ("| || | || | | || \ || \ ") print ("|__ __|| | || __| | __|| \ || \ ") print (" | | | || __) | __)| || D )") print (" | | | | || | | || \ || / ") print (" |__| |__|__||____| |____||___\__||____/ ") print()
quit=input("Press Enter to exit")
Edited by teknix (10/11/13 11:58 AM)
|
teknix
πβπ
’ππ
π°π‘ πΌπ⨻



Registered: 09/16/08
Posts: 11,953
|
Re: Learning How to program with Python. [Re: Penelope_Tree]
#18944112 - 10/07/13 10:37 AM (10 years, 4 months ago) |
|
|
Quote:
Penelope_Tree said: I'm learning Python for a free, online programming class (Bioinformatics - I'm really excited about it and hope I can keep up with it and a full-time job) I'm embarrassed to say that I'm having difficulty getting it running. I keep searching for answers online, but all I get are long, manual-type documents. Would somebody be willing to help me out here?
Thanks for making this thread, teknix. I'm going to go back through it when I figure out how to get Python working & try to follow along with the exercises you provided.
If you search for "Idle" in the start menu it should give u the shell when you click on it. It says Idle (python gui)
Then to open the editor you click on "file" and select "new window".
Then I usually change the setting to open the editor on startup in "Options" -> "configure idle" -> general tab, and click the tick.
|
teknix
πβπ
’ππ
π°π‘ πΌπ⨻



Registered: 09/16/08
Posts: 11,953
|
Re: Learning How to program with Python. [Re: teknix]
#18944120 - 10/07/13 10:39 AM (10 years, 4 months ago) |
|
|
I realized if I put it in code you can't just copy paste it into python, so I took out the code.
Edited by teknix (10/11/13 12:02 PM)
|
teknix
πβπ
’ππ
π°π‘ πΌπ⨻



Registered: 09/16/08
Posts: 11,953
|
Re: Learning How to program with Python. [Re: teknix]
#18952839 - 10/09/13 04:48 AM (10 years, 4 months ago) |
|
|
Here is a simple encoder and decoder:
new = ("") value = ("") result = ("") decode = ("") rotation = 1
while True: userString = input("Please enter a command, e to encode, d to decode, q to quit. ") if userString == "q": break if userString != "d" and userString != "e": print("Invalid option selected, please try again") elif userString =="e": original = input("Please enter a string to encode. ") rotation = int(input("Please enter the rotation value 1 - 25. ")) if rotation <=0 or rotation > 25: print("Invalid rotation value, please try again.") if rotation > 0 and rotation < 26: for char in original: value = ord(char) if char>="a" and char<="z": new = (value + rotation) if new > 122: new = new - 26 result = str(result) + chr(new) new = ("") elif new <= 122: result = str(result) + chr(new) new = ("") elif char>="A" or char<="Z": new = (value) result = str(result) + chr(new) new = ("") if len(result) == len(original): print(result) result = ("") new = ("") elif userString == "d": decode = input("Please enter a string to decode. ") rotation = int(input("Please enter the rotation value 1 - 25. ")) if rotation < 0 or rotation > 25: print("Invalid rotation value, please try again.") if rotation >= 0 and rotation < 26: for char in decode: value = ord(char) if char>="a" and char<="z": new = (value - rotation) if new <= 96: new = new + 26 result = str(result) + chr(new) new = ("") elif new > 96: result = str(result) + chr(new) new = ("") elif char>="A" or char<="Z": new = (value) result = str(result) + chr(new) new = ("") if len(decode) == len(result): print(result) result = ("") new = ("") quit=input("Press enter to quit. ")
Edited by teknix (10/11/13 12:44 PM)
|
Penelope_Tree
Shamanic Panic



Registered: 07/31/09
Posts: 8,535
Loc: magic sugarcastle
|
Re: Learning How to program with Python. [Re: teknix]
#18960034 - 10/10/13 03:38 PM (10 years, 4 months ago) |
|
|
Thanks!! For whatever reason, I wasn't seeing Idle (I think I got confused by the website saying some versions needed an outside-idler). Anyway, I got it up & running and am learning ^_^ You are leaps & bounds ahead of me, but I'm gonna catch up to you!
If anyone wants to learn, too, Coursera is running a free Introductory course.
--------------------
full blown human
|
Amphibolos
Le bourgeois gentilhomme




Registered: 05/22/09
Posts: 626
|
Re: Learning How to program with Python. [Re: Penelope_Tree]
#18961385 - 10/10/13 08:49 PM (10 years, 4 months ago) |
|
|
I use a guide for the non programmer, if it interest you.
Its really handy
...CHecking coursera. Wondering if i can fit this in my university schedule
--------------------
"Homo sum ; humani nihil a me alienum puto"
Edited by Amphibolos (10/10/13 08:55 PM)
|
teknix
πβπ
’ππ
π°π‘ πΌπ⨻



Registered: 09/16/08
Posts: 11,953
|
Re: Learning How to program with Python. [Re: Amphibolos]
#18963833 - 10/11/13 12:46 PM (10 years, 4 months ago) |
|
|
Here is another code like the one above, but with improved error checking and a Brute Force cracker implemented. ^.^
new = ("") value = ("") result = ("") rotation = 1 crackValue = ("") test = ("") cracked = False crack = ""
while True: print("Enter e to encode a string.") print("Enter d to decode a string.") print("Enter c to crack a string.") print("Enter q to quit") userString = input("Please enter a command: ") if userString == "q": break if userString != "d" and userString != "e" and userString != "c": print("Invalid option selected, please try again") print("-----------------------------------------") elif userString =="e": original = input("Please enter a string to encode. ") rotation = input("Please enter the rotation value 1 - 25. ") while(not(rotation.isdigit())) or (int(rotation) < 1 \ or int(rotation) > 25): print("Invalid input, please try again.") print("-----------------------------------------") break else: rotation = int(rotation) for char in original: value = ord(char) if char>="a" and char<="z": new = (value + rotation) if new > 122: new = new - 26 result = str(result) + chr(new) new = ("") elif new <= 122: result = str(result) + chr(new) new = ("") elif char>="A" or char<="Z": new = (value) result = str(result) + chr(new) new = ("") if len(result) == len(original): print(result) result = ("") new = ("") elif userString == "d": decode = input("Please enter a string to decode. ") rotation = input("Please enter the rotation value 1 - 25. ") while(not(rotation.isdigit())) or (int(rotation) < 1 \ or int(rotation) > 25): print("Invalid input, please try again.") print("-----------------------------------------") break else: rotation = int(rotation) for char in decode: value = ord(char) if char>="a" and char<="z": new = (value - rotation) if new <= 96: new = new + 26 result = str(result) + chr(new) new = ("") elif new > 96: result = str(result) + chr(new) new = ("") elif char>="A" or char<="Z": new = (value) result = str(result) + chr(new) new = ("") if len(decode) == len(result): print(result) result = ("") new = ("") elif userString == "c": crack = input("Please enter a string to crack.") given = input("Please enter a word in the string.") for rotationValue in range(1,26): for char in crack: if ord(char) > 96 and ord(char) < 123: new = ord(char) - rotationValue if new < ord("a"): new = new + 26 result = result + chr(new) else: result = result + char if result.find(given) == -1: result = ("") else: cracked=True break if cracked: print("Decoded string ",result) print("Rotation: ", rotationValue) else: print("No crack found.")
quit=input("Press enter to quit. ")
|
teknix
πβπ
’ππ
π°π‘ πΌπ⨻



Registered: 09/16/08
Posts: 11,953
|
Re: Learning How to program with Python. [Re: teknix]
#18984607 - 10/16/13 02:12 AM (10 years, 4 months ago) |
|
|
The values of 97 and 122 represent a and z respectively on the ascii/Unicode table, so I'm telling it to stay within that range.
I'm using ord() to change a character to the ascii/Unicode values and I'm using chr to change the value back into a character.
You could just as well use "a" instead of the ord() function.
the len() function is used to get the length.
|
|