I have made this simple password generator with the option to choose the length of the password. It seems to randomly add the length to itself however, so if I keep choosing a length of 5 it will occasionally output a password of 10 characters and then go back to 5.
import random
randomnums = []
checker = [0, 0, 0, 0] ##Make sure there are upper, lower case, symbols and numbers.
while True:
length = input("How long do you want your password?: ")
if length.isdigit() == False:
print("please enter a number..\n")
elif int(int(length)) < 4:
print("Please enter a length of 4 or greater...\n")
else:
break
while checker[0] == 0 or checker[1] == 0 or checker[2] == 0 or checker[3] == 0:
for i in range(0,int(length)):
x = random.randrange(0, 4)
if x == 1:
randomnums.append(random.randrange(0, 10))
checker[0] = 1
elif x == 2:
randomnums.append(random.choice('abcdefghijklmnopqrstuvwxyz'))
checker[1] = 1
elif x == 3:
randomnums.append(random.choice('ABCDEFGHIJKLMNPQRSTUVWXYZ'))
checker[2] = 1
else:
randomnums.append(random.choice('!##$%^&*()_+:"}|<>?'))
checker[3] = 1
password = ''.join(map(str, randomnums)) ##join list to string
print(password)`
You could get any multiple of length with this code. It adds length characters in the body until you got the required length. You should instead generate a password and check it, and if it is bad, repeat.
When I assign the out_of_marks_limit = True in 5th block, I want the first block of if statement to be "True" and I don't want the code to loop or ask the user anymore.
In other programming language indentation is used to make the program look good. But because python only checks the condition of first block to the same indent, I can't assign boolean of 2 block of code to the first block.
This is what I'm trying to say.
I'm an intermediate level programmer and this program is just for practice purpose.
a = int(input("Enter no. of subjects: "))
count = 1
d = 0
out_of_marks_limit = False #I want the out_of_limit to be True
if a <= 10:
if not out_of_marks_limit:
if count <= a:
for c in range(a):
b = float(input("Enter mark of subject " + str(count) + ": "))
if b <= 100: #If this condition went false then it will skip to else statement
d += b
count += 1
if count > a:
cal = round(d/a, 2)
print("Your percentage is " + str(cal) + "%")
else:
out_of_marks_limit = True #So this boolean value should passed to the first line of code
print("Marks enter for individual subject is above 100")
else:
print("Subject limit exceeded")
I expect the output to print("Marks enter for individual subject is above 100"), if out_of_marks_limit is True and don’t want to loop anymore
I think you can use a while loop to check your out_of_marks_limit condition:
a = int(input("Enter no. of subjects: "))
count = 1
d = 0
out_of_marks_limit = False #I want the out_of_limit to be True
while not out_of_marks_limit:
if a <= 10:
if not out_of_marks_limit:
if count <= a:
for c in range(a):
b = float(input("Enter mark of subject " + str(count) + ": "))
if b <= 100: #If this condition went false then it will skip to else statement
d += b
count += 1
if count > a:
cal = round(d/a, 2)
print("Your percentage is " + str(cal) + "%")
else:
out_of_marks_limit = True #So this boolean value should passed to the first line of code
print("Marks enter for individual subject is above 100")
else:
print("Subject limit exceeded")
I already solved this with list.append() function however my instructor told me to just use the basic python functions. Here is my code:
a = 0
b = 0
s = 0
x = str(s)
print ('Enter the first number: ', end = '')
c = input()
a = int(c)
finished = False
while not finished:
print ('Enter the next number (0 to finish): ', end ='')
n = input()
b = int(n)
if b != 0:
if b == a:
x = ('Same')
elif b > a:
x = ('Up')
elif b < a:
x = ('Down')
a = b
s = x
else:
finished = True
print (str(x))
I am aiming to print (e.g. Up Down Up Down Same in comparing the input integers) in one line at the end of the while loop. Let me know how can I improve my code. Thank you very much
Use string concatenation to get the result you want without using a list:
http://www.pythonforbeginners.com/concatenation/string-concatenation-and-formatting-in-python
I'll give you two hints on how to do this for your program:
Initialize x as an empty string by replacing
x=str(s)
with
x=""
There is no need for it to begin as the string "0", which str(s) does since s is 0.
Instead of saying
x=('SAME')
x=('UP')
x=('DOWN')
try saying
x=x+'SAME'
x=x+'UP'
x=x+'DOWN'
I removed the parentheses because they are not necessary.
As for style, it is good practice to name your variables as useful things instead of just letters. Last staement in an if/else chain that covers all bases should just be else. Best of luck to you sir
Not sure what result you're looking for, but perhaps this works:
a = 0
b = 99
result = ""
a = int(input('Enter the first number: '))
while b != 0:
b = int(input('Enter the next number (0 to finish): '))
if b == a:
result += ' Same'
elif b > a:
result += ' Up'
elif b < a:
result += ' Down'
a = b
print(result.strip())
Output:
Enter the first number: 12
Enter the next number (0 to finish): 12
Enter the next number (0 to finish): 12
Enter the next number (0 to finish): 1
Enter the next number (0 to finish): 1
Enter the next number (0 to finish): 5
Enter the next number (0 to finish): 0
Same Same Down Same Up Down
You can simply initialize x with an empty string and keep concatenating to it.
a = 0
b = 0
s = 0
x = ''
print('Enter the first number: ', end='')
c = input()
a = int(c)
finished = False
while not finished:
print('Enter the next number (0 to finish): ', end='')
n = input()
b = int(n)
if b != 0:
if b == a:
x += 'Same\n'
elif b > a:
x += 'Up\n'
elif b < a:
x += 'Down\n'
a = b
s = x
else:
finished = True
print(str(x))
I get this error "TypeError: Can't convert 'int' object to str implicitly"
when I try to run this code. it's on the x += 1 line.
The goal of this program is to receive input such as "RA1" or "R04" and add 1 to the value, which always starts at 0. I then want it to "print" the results when I type END. This is proving to be quite the challenge. Python 3.5.1
x = 0
y = 0
z = 0
print("Enter 3 digit code.")
x = str(input())
while x != "END":
if x == "RA1":
x += 1
continue
elif x == "R04":
y += 1
continue
elif x == "etc":
z += 1
continue
else:
print('Please enter a 3 digit value or END')
There were multiple problems in your code.
The cause of the TypeError was that you unintentionally made x a string by writing x = str(input()) and when you wanted to add 1 to it, Python tried to convert 1 to a string to concatenate it to x.
The infinite loop was unrelated to the TypeError you received. It occurred because of the misplacement of the str(input()) call.
This is the working code with the fixed while loop:
x = 0
y = 0
z = 0
i = ""
print("Enter 3 digit code.")
while i != "END":
i = input()
if i == "RA1":
x += 1
elif i == "R04":
y += 1
elif i == "etc":
z += 1
else:
print('Please enter a 3 digit value or END')
print(str(x) + " " + str(y) + " " + str(z))
You need to cast your integers into strings by wrapping each with str().
Afterwards, use x = raw_input() for your inputs. When you first enter the loop, you need to take an input, but also when you loop back around. So, therefore, you need a way to stop your infinite loop. Hence, x = raw_input() needs to be added at the start of the loop.
You can actually just get your input at the start of the loop, there's no need for 2.
Additionally, your x, y and z should also just be empty strings or null, not 0.
x = ""
y = ""
z = ""
while x != "END":
print("Enter 3 digit code.")
x = raw_input()
if x == "RA1":
x += str(1)
continue
if x == "R04":
y += str(1)
continue
elif x == "etc":
z += str(1)
continue
else:
print('Please enter a 3 digit value or END')
I'm VERY new to programming (as in, in an intro class) and python. My assignment is to prompt a user to answer math questions, count the number they got correct/incorrect, calculate/display their numeric grade, and display their letter grade.
Everything seems to be working fine.. except I can't figure out how to get it to count the number of correct/incorrect answers. Any help?
def main():
name = input("What is your name? ")
correct = 0
incorrect = 0
ans = int(input("What is 4 + 5? "))
val = add(4, 5)
if(ans == val):
correct + 1
else:
incorrect + 1
ans2 = int(input("What is 20 * 6? "))
val2 = mult(20, 6)
if(ans2 == val2):
correct + 1
else:
incorrect + 1
ans3 = int(input("What is 14 - 10? "))
val3 = sub(14, 10)
if(ans3 == val3):
correct + 1
else:
incorrect + 1
ans4 = int(input("What is 30 / 5? "))
val4 = div(30, 5)
if(ans4 == val4):
correct + 1
else:
incorrect + 1
ans5 = int(input("What is 29 + 2? "))
val5 = add(29, 2)
if(ans5 == val5):
correct + 1
else:
incorrect + 1
ans6 = int(input("What is 50 - 10? "))
val6= sub(50, 10)
if(ans6 == val6):
correct + 1
else:
incorrect + 1
ans7 = int(input("What is 5 * 11? "))
val7 = mult(5, 11)
if(ans7 == val7):
correct + 1
else:
incorrect + 1
ans8 = int(input("What is 9 / 3? "))
val8 = div(9, 3)
if(ans8 == val8):
correct + 1
else:
incorrect + 1
ans9 = int(input("What is 90 - 5? "))
val9 = sub(90, 5)
if(ans9 == val9):
correct + 1
else:
incorrect + 1
ans10 = int(input("What is 412 + 5? "))
val10 = add(412, 5)
if(ans10 == val10):
correct + 1
else:
incorrect + 1
print()
print("Thanks, " + str(name) + "!")
print()
print("Correct " + str(correct))
print()
print("Incorrect " + str(incorrect))
print()
calcGrade(correct)
def add(value, value2):
return value + value2
def sub(value, value2):
return value - value2
def mult(value, value2):
return value * value2
def div(value, value2):
return value / value2
def calcGrade(correct):
grade = (correct * 100)/ 10
print("Numeric Grade " + str(grade))
if(grade > 90):
letterGrade = "A"
if(grade > 80):
letterGrade = "B"
if(grade < 70):
letterGrade = "C"
if(grade < 69):
letterGrade = "F"
print()
print("Letter Grade " + str(letterGrade))
main()
When you write correct + 1 you are evaluating what correct plus one is equal to, but you aren't updating the value stored in correct.
What you want to put instead is correct = correct + 1. Or, more succinctly, correct += 1.
The same applies to incorrect.
Every programming language has some specific features which you can use for different purposes. In Python, there is this function called eval() which is very useful for your case ...
Here is an example if its use:
In [2]: eval('2*3'), eval('12+13'), eval('30/5')
Out[2]: (6, 25, 6)
Notice that you do the same set of operations many times:
Print a prompt
Input a number
evaluate another number
see of the two numbers are equal
increment respective counters
In Python (or in many other languages), these would be done in a loop. For this, you need to create a list if things you want to loop over. The list will look like this:
In [4]: qns
Out[4]:
['4+5',
'20*6',
'14-10',
'30/5',
'29+2',
'50-10',
'5*11',
'9/3',
'90-5',
'412+5']
At this point, you can use something called list comprehension to get all the results ...
In [5]: [input('what is ' + q + '?') for q in qns ]
what is 4+5?9
what is 20*6?12
what is 14-10?34
what is 30/5?6
what is 29+2?31
what is 50-10?40
what is 5*11?50
what is 9/3?2
what is 90-5?85
what is 412+5?417
Out[5]: [9, 12, 34, 6, 31, 40, 50, 2, 85, 417]
Now you need to compare them with the actual values. You can actually put the comparison within the list comprehension into a single operation.
In [6]: results = [input('what is ' + q + '?') == eval(q) for q in qns ]
what is 4+5?9
what is 20*6?12
what is 14-10?34
what is 30/5?6
what is 29+2?31
what is 50-10?40
what is 5*11?50
what is 9/3?2
what is 90-5?85
what is 412+5?417
In [7]: results
Out[7]: [True, False, False, True, True, True, False, False, True, True]
In Python, it turns out that True == 1 and False == 0 for some cases. Which cases you ask? Well, that's something that comes with experience in something called duck typing. So in a couple of months, with enough experience, you will find the answer to "which cases" almost trivial. Anyway, because of this phenomenon, you can count the right answers as:
In [8]: sum(results)
Out[8]: 6
And the incorrect answers?
In [9]: len(qns) - sum(results)
Out[9]: 4
Cheers, and happy programing!
you have some mistakes with your code. Please try to this. correct +=1 or correct = correct + 1 and incorrect -=1 and incorrect = incorrect - 1
correct = correct + 1
incorrect = incorrect + 1
By doing this, the current value of the correct/incorrect answers is incremented and stored in the same variable, basically, this is a counter, for the number of correct and incorrect answers