I'm trying to make a random generator of numbers and letters go into one line after being in a string, for example from:
a
b
c
Like so...:
To "abc"
I was wondering if there was any way of doing that. The code I use is:
import random
import time
amount = int(input("How many digits do you want the password to be?\n"))
time.sleep(0.5)
print("============================================================\n")
for count in range(amount):
time.sleep(0.1)
num = random.randint(1,62)
if num == 1:
a = print("1")
if num == 2:
print("2")
if num == 3:
print("3")
if num == 4:
print("4")
if num == 5:
print("5")
if num == 6:
print("6")
if num == 7:
print("7")
if num == 8:
print("8")
if num == 9:
print("9")
if num == 10:
print("a")
if num == 11:
print("b")
if num == 12:
print("c")
if num == 13:
print("d")
if num == 14:
print("e")
if num == 15:
print("f")
if num == 16:
print("g")
if num == 17:
print("h")
if num == 18:
print("i")
if num == 19:
print("j")
if num == 20:
print("k")
if num == 21:
print("l")
if num == 22:
print("m")
if num == 23:
print("n")
if num == 24:
print("o")
if num == 25:
print("p")
if num == 26:
print("q")
if num == 27:
print("r")
if num == 28:
print("s")
if num == 29:
print("t")
if num == 30:
print("u")
if num == 31:
print("v")
if num == 32:
print("w")
if num == 33:
print("x")
if num == 34:
print("y")
if num == 35:
print("z")
if num == 36:
print("A")
if num == 37:
print("B")
if num == 38:
print("C")
if num == 39:
print("D")
if num == 40:
print("E")
if num == 41:
print("F")
if num == 42:
print("G")
if num == 43:
print("H")
if num == 44:
print("I")
if num == 45:
print("J")
if num == 46:
print("K")
if num == 47:
print("L")
if num == 48:
print("M")
if num == 49:
print("N")
if num == 50:
print("O")
if num == 51:
print("P")
if num == 52:
print("Q")
if num == 53:
print("R")
if num == 54:
print("S")
if num == 55:
print("T")
if num == 56:
print("U")
if num == 57:
print("V")
if num == 58:
print("W")
if num == 59:
print("X")
if num == 60:
print("Y")
if num == 61:
print("Z")
This basically creates the string of how many characters you want but I can't get them all on one line.
You can use the string module.
Example Code:
import string
print(string.ascii_letters+string.digits)
You can then get a certain number of random letters and numbers using this code:
import random
import string
number_of_letters_and_digits = 5
print(''.join([random.choice(string.ascii_letters+string.digits) for i in range(number_of_letters_and_digits)]))
Completed Code:
import random
import string
import time
amount = int(input("How many digits do you want the password to be?\n"))
time.sleep(0.5)
print("============================================================\n")
print(''.join([random.choice(string.ascii_letters+string.digits) for i in range(amount)]))
If you need to use the code that you showed, you could append the letters to the string (replace print("letter") with listName.append("letter")). Then, after the for loop, you could run print(''.join(listName)).
Related
while True:
month_num = int(input("enter the month number: "))
if month_num == 1:
print("january")
elif month_num == 2:
print("February")
elif month_num == 3:
print("March")
elif month_num == 4:
print("April")
elif month_num == 5:
print("May")
elif month_num == 6:
print("June")
elif month_num == 7:
print ("July")
elif month_num == 8:
print ("August")
elif month_num == 9:
print ("September")
elif month_num == 10:
print ("October")
elif month_num == 11:
print ("November")
elif month_num == 12:
print ("December")
else:
print("Enter a valid number")
i want to end the code cleanly but dont know to go about doind it i just stared it but i want to do it so that if i do put in the vlaues from 1 to 12 it would break after but i dont know how to do it efficently
import calendar
while True:
try:
month_num = int(input("enter the month number: "))
if 0 < month_num < 13:
print(calendar.month_name[month_num])
break
except ValueError:
pass
I'm creating a program which I have to put a number and when I run the program, the solution should be the day for that number. But I don't know how to do. The program I did was this:
num = 4
if(num = 1)
print('Monday')
elif(num = 2)
print('Tuesday')
elif(num = 3)
print('Wednesday')
elif(num = 4)
print('Thursday')
elif(num = 5)
print('Friday')
elif(num = 6)
print('Sunday')
elif(num = 7)
print('Saturday')
elif(num < 1)
print('Put a number between 1 and 7')
else(num > 7)
print('Put a number between 1 and 7')
In python, for statements like if, for, etc. You have to add : at the end of it.
And for comparing (for equal) you have to use == and not =
num = 4
if(num == 1):
print('Monday')
elif num == 2:
print('Tuesday')
.....
You can compare without parenthesis and it will work too.
num = 0
while num <= 7:
num += 1
if num == 1:
print('Monday')
elif num == 2:
print('Tuesday')
elif num == 3:
print('Wednesday')
elif num == 4:
print('Thursday')
elif num == 5:
print('Friday')
elif num == 6:
print('Saturday')
elif num == 7:
print('Sunday')
I am using Python 3.5 idle.
I am getting an indentation error while using elif function.
This is the code that I've entered:
>>> num = 7
>>> if num == 5:
print("number is 5")
... elif num == 11:
File "<stdin>", line 2
elif num == 11:
^
IndentationError: expected an indented block
You need to write some code after your elif. For example:
>>> num = 7
>>> if num == 5:
... print("Number is 5")
... elif num == 11:
... print("Number is 11")
...
>>> if num == 5:
... print("Number is 5")
... elif num == 11:
... print("Number is 11")
... elif num == 7:
... print("Number is 7")
...
Number is 7
>>>
This happens when you press enter after elif num == 11:
>>> if num == 5:
... print("Number is 5")
... elif num == 11:
...
File "<stdin>", line 4
^
IndentationError: expected an indented block
>>>
If you don't want to do anything for some reason in that case use pass
>>> num = 11
>>> if num == 5:
... print("Number is 5")
... elif num == 11:
... pass
... elif num == 7:
... print("Number is 7")
...
>>>
I am doing euler problems to practice python, I can't seem to get the right answer for problem 17. The problem is to find the number of letters in all numbers 1-1000 without spaces or hyphens.
Any comment would be useful.
Python code:
def numberToString(number):
""" Writes a cardinal number to a string. """
numberString=""
c = str(number)
l = len(c)
while l>0:
if l == 1: #unit digit
if int(c[-1]) == 1:
numberString += 'one'
elif int(c[-1]) == 2:
numberString += 'two'
elif int(c[-1]) == 3:
numberString += 'three'
elif int(c[-1]) == 4:
numberString += 'four'
elif int(c[-1]) == 5:
numberString += 'five'
elif int(c[-1]) == 6:
numberString += 'six'
elif int(c[-1]) == 7:
numberString += 'seven'
elif int(c[-1]) == 8:
numberString += 'eight'
elif int(c[-1]) == 9:
numberString += 'nine'
l = l-1
elif l == 2 and int(c[-2])==1 and int(c[-1]) != 0: #teens
if int(c[-1]) == 1:
numberString += 'eleven'
elif int(c[-1]) == 2:
numberString += 'twelve'
elif int(c[-1]) == 3:
numberString += 'thirteen'
elif int(c[-1]) == 4:
numberString += 'fourteen'
elif int(c[-1]) == 5:
numberString += 'fifteen'
elif int(c[-1]) == 6:
numberString += 'sixteen'
elif int(c[-1]) == 7:
numberString += 'seventeen'
elif int(c[-1]) == 8:
numberString += 'eighteen'
elif int(c[-1]) == 9:
numberString += 'nineteen'
l = l-2
elif l == 2: #tens
if int(c[-2]) == 1:
numberString += 'ten'
elif int(c[-2]) == 2:
numberString += 'twenty'
elif int(c[-2]) == 3:
numberString += 'thirty'
elif int(c[-2]) == 4:
numberString += 'fourty'
elif int(c[-2]) == 5:
numberString += 'fifty'
elif int(c[-2]) == 6:
numberString += 'sixty'
elif int(c[-2]) == 7:
numberString += 'seventy'
elif int(c[-2]) == 8:
numberString += 'eighty'
elif int(c[-2]) == 9:
numberString += 'ninety'
if int(c[-1]) != 0 and int(c[-2]) > 1:
numberString += '-'
l = l-1
elif l == 3: #hundreds
if int(c[-3]) == 1:
numberString += 'one'
elif int(c[-3]) == 2:
numberString += 'two'
elif int(c[-3]) == 3:
numberString += 'three'
elif int(c[-3]) == 4:
numberString += 'four'
elif int(c[-3]) == 5:
numberString += 'five'
elif int(c[-3]) == 6:
numberString += 'six'
elif int(c[-3]) == 7:
numberString += 'seven'
elif int(c[-3]) == 8:
numberString += 'eight'
elif int(c[-3]) == 9:
numberString += 'nine'
if int(c[-3]) != 0:
numberString += ' hundred'
if int(c[-1])+int(c[-2]) != 0:
numberString += ' and '
l = l-1
elif l == 4: #thousands
if int(c[-4]) == 1:
numberString += 'one'
elif int(c[-4]) == 2:
numberString += 'two'
elif int(c[-4]) == 3:
numberString += 'three'
elif int(c[-4]) == 4:
numberString += 'four'
elif int(c[-4]) == 5:
numberString += 'five'
elif int(c[-4]) == 6:
numberString += 'six'
elif int(c[-4]) == 7:
numberString += 'seven'
elif int(c[-4]) == 8:
numberString += 'eight'
elif int(c[-4]) == 9:
numberString += 'nine'
if int(c[-4]) != 0:
numberString += ' thousand'
if int(c[-3]) != 0:
numberString += ' '
l = l-1
return numberString
def NumbersInString(min, max):
""" Writes all cardinal numbers on a seperate line on a string, from min to max. """
allNumberString=""
for i in range (min,max+1):
allNumberString += numberToString(i)
allNumberString += '\n'
return allNumberString
def stringNumberCount(numberString):
""" Returns the ammount of letters without spaces, hyphens or newlines. """
numberString=numberString.replace(' ', '')
numberString=numberString.replace('-', '')
numberString=numberString.replace('\n', '')
return len(numberString)
def run(filename, min=1, max=1000):
s=NumbersInString(min, max)
string='Number of letters without spaces or hyphens: '+str(stringNumberCount(s))+'\n'+'List of cardinal numbers:\n'
string += s
f=open(filename, 'w')
f.write(string)
f.close
run('output.txt')
first you change fourty to forty (as written in problem statement)
and second
in elif l==3: block
why r you writing if int(c[-3]!=0):
since we have already covered those cases just before (i mean c[-3]==1,2,3... are not equal to zero )
correct me if i'm wrong and try to put some comment in your code.
http://ideone.com/egeX0k
def countLetter(n):
p=['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine']
q=['','Ten','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety']
r=['','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen']
c=str(n)
l=len(c)
a=""
if l==1:
a+=p[int(c[0])]
elif l==2:
if c[1]=='0':
a+=q[int(c[0])]
elif c[0]=='1':
a+=r[int(c[1])]
else:
a+=q[int(c[0])]+p[int(c[1])]
elif n==100:
a+="OneHundred"
elif l==3:
a+=p[int(c[0])]+"Hundred"
if c[1]=='0' and c[2]=='0':
a+=""
elif c[2]=='0' and c[1]!='0':
a+="And"+q[int(c[1])]
elif c[1]=='1':
a+="And"+r[int(c[2])]
else:
a+="And"+q[int(c[1])]+p[int(c[2])]
else:
a+="OneThousand"
#print a,len(a)
return len(a)
def main():
sum=0
for i in range(1,1001):
sum+=countLetter(i)
print sum
main()
This block of code returns "cat", "dog", "hamster", and "unicorn", but it shouldn't return "unicorn" at all! Is there any reason for this?
if random.randint(0,10) < 5:
print("dog")
elif random.randint(0,10) > 5:
print("cat")
elif random.randint(0,10) == 5:
print("hamster")
else:
print("unicorn")
You're getting new random number on each comparison. What you probably meant is:
my_random_int = random.randint(0,10)
if my_random_int < 5:
print("dog")
elif my_random_int > 5:
print("cat")
elif my_random_int == 5:
print("hamster")
else:
print("unicorn")
random.randint is called again each time it is reached, potentially producing a different result each time (since that is the function's purpose).
If you want to repeatedly test with the same value, then store the value first.
You should create the random number only once!
val = random.randint(0,10)
if val < 5:
print("dog")
elif val > 5:
print("cat")
elif val == 5:
print("hamster")
else:
print("unicorn")
Assuming correct indentation, there's no reason for three random ints to be respectively >=5, <=5, and "not 5".
You probably meant to do this:
value = random.randint(0, 10)
if value < 5:
print("dog")
elif value > 5:
print("cat")
elif value == 5:
print("hamster")
else:
print("unicorn")
Now there are no chances of unicorns.
Your random number is different everytime you call random.randint so it might be 7 when you test the first if and go past it, then 3, then 4, and bam, you're in unicorn.
You should call random.randint only once at the beginning of your if, save its value and check it instead.
myrand = random.randint(0,10)
if myrand < 5:
print("dog")
elif myrand > 5:
print("cat")
elif myrand == 5:
print("hamster")
else:
print("unicorn")
The issue here is that you're generating a new random number each time. You should create it once and then assign it to a variable, then check that.
You're generating three different random numbers. What you're thinking is this:
random_number = random.randint(0,10)
if random_number < 5:
print("dog")
elif random_number > 5:
print("cat")
elif random_number == 5:
print("hamster")
else:
print("unicorn")
This code will only return one word, and will never return "unicorn".
You need to create only one random integer.
Your code should be:
myRandom = random.randint(0,10)
if myRandom < 5:
print("dog")
elif myRandom > 5:
print("cat")
elif myRandom == 5:
print("hamster")
else:
print("unicorn")