Min första "riktiga" kodsnutt i Python, tips ock kritik sökes
Jag håller på att försöka lära mig lite Python, dels för att det är ett roligt språk och dels för att jag ska läsa scriptprogrammering i februari nästa år, så jag tänkte kasta ut den första "riktiga" koden jag lyckats knacka ihop och mottager gärna tips samt konstruktiv kritik. Jag har ingen tidigare erfarenhet av programmering förutom visual basic som jag läste på gymnasiet för 7 år sedan, läser även C# för tillfället.
Håll till godo!
Jag vet att den är "slafsig" i brist på bättre uttryck, men det är ju också därför jag postar.
Jag letar speciellt efter bra tips på hur man kan begränsa input till en given grupp karaktärer som accepteras som korrekta input's, dvs bara 0 - 9 accepteras.
__author__ = 'Zoap'
def function():
import math
yes = "y" or "Y"
no = "n" or "N"
inv = "Invalid input"
invx = "Number to search for must be a number!"
invs = "Length must be a number!"
space = "No spacing allowed!"
number_list = []
print()
print("This program allows you to search for the number of possible combinations of a pin number of length x, "
"given that you know one of the digits")
print()
x = str(input("Input number to search for: "))
if x == '':
print()
print(invx)
function()
return
elif ' ' in x:
print()
print(space)
function()
return
else:
print()
def function2():
s = str(input("Input length of pin code: "))
stop = int((math.pow(10, int(s))))
if s == '':
print()
print(invs)
print()
function2()
return
elif ' ' in s:
print()
print(space)
print()
function2()
return
elif int(s) > 7:
print()
print("Length can not be more than 7 digits!")
print()
function2()
return
elif stop < int(x):
print()
print("Length of pin makes the number to search for bigger than the possible number of variations!")
print()
function2()
return
else:
# For every integer in range add int to list and convert to string
for i in range(0, (stop + 1)):
number_list.append(str(i))
print()
# For every str in list containing x, count x
x_numbers = [a for a in number_list if x]
# Percent of total numbers in range containing digit x
percent = len(x_numbers) / (stop / 100)
if int(x) >= 10:
print("There are a total of " + str(len(x_numbers)) +
" numbers between 0 and " + str(stop) + " containing the number " + x)
print()
print("That is a total of " + str("%.2f" % percent) + "%" " of all numbers between 0 and " +
str(stop))
elif int(len(x_numbers)) == 1:
print("There are a total of " + str(len(x_numbers)) +
" number between 0 and " + str(stop) + " containing the digit " + x)
print()
print("That is a total of " + str("%.2f" % percent) + "%" " of all numbers between 0 and " +
str(stop))
else:
print("There are a total of " + str(len(x_numbers)) +
" numbers between 0 and " + str(stop) + " containing the digit " + x)
print()
print("That is a total of " + str("%.2f" % percent) + "%" " of all numbers between 0 and " +
str(stop))
def function3():
print()
choice = input("Run again? y/n: ")
if choice == yes:
print()
function()
return
elif choice == no:
exit()
else:
print()
print(inv)
function3()
return
while True:
function3()
while True:
function2()
while True:
function()
"Life is a sexually transmitted disease and the mortality rate is 100%." - R. D. Laing