import math
def roundup(x):
return int(math.ceil(x / 10.0)) * 10
w=0
while w == 5:
print("Would you like to *work out* a missing letter in a GTIN-8 code, or *check* a code?")
response = input(":")
if response == 'work out':
print("Input a 7 digit GTIN-8 code and I'll work out the 8th")
c1 = int(input("Enter FIRST number: "))
c2 = int(input("Enter SECOND number: "))
c3 = int(input("Enter THIRD number: "))
c4 = int(input("Enter FOURTH number: "))
c5 = int(input("Enter FIFTH number: "))
c6 = int(input("Enter SIXTH number: "))
c7 = int(input("Enter SEVENTH number: "))
y = (c1*3+c2+c3*3+c4+c5*3+c6+c7*3)
ru2=roundup(y)
GTIN8 = ru2-y
print("Your GTIN8 Code would be: "+str(c1)+str(c2)+str(c3)+str(c4)+str(c5)+str(c6)+str(c7)+str(GTIN8))
print("Wanna work out another?")
if response == 'check':
print("Input a 8 digit GTIN-8 code and I'll check if it's correct")
c1 = int(input("Enter FIRST number: "))
c2 = int(input("Enter SECOND number: "))
c3 = int(input("Enter THIRD number: "))
c4 = int(input("Enter FOURTH number: "))
c5 = int(input("Enter FIFTH number: "))
c6 = int(input("Enter SIXTH number: "))
c7 = int(input("Enter SEVENTH number: "))
c8 = int(input("Enter EIGTH number: "))
y = (c1*3+c2+c3*3+c4+c5*3+c6+c7*3)
ru2=roundup(y)
GTIN8 = ru2-y
if GTIN8 != c8:
print("Nope that product code is incorrect!")
reply=input("Want to know the correct answer to your code? Type yes if so: ")
if reply == 'yes':
print("The correct answer would have been: "+str(GTIN8))
if GTIN8 == c8:
print("That code is correct!")
The problem is that I have tried time and time again, to make this code smaller.
Even by inputting 'response' as a string, to allow the user to type the code in one go.
If you couldn't already tell, this is a code for a GTIN-8 product code and I know there are various other GTIN-8 codes out there but I just couldn't do it, let alone copy.
To get you started, here is one simple way of reducing the number of lines
c = [int(x) for x in input("Input a 8 digit GTIN-8 code and I'll check if it's correct").split("")]
Now you can access each character with c[n].
Related
I feel stupid for already having to ask questions this early into learning python but the task is this:
Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below.
I have not started on the try/except part yet but with my current code it seems to only be running the full loop once then getting stuck on the largest number check.
largest = None
smallest = None
while True:
num = input("Enter a number: ")
if num == "done" : break
if largest is None or num > largest :
largest = num
print("large", largest)
if smallest is None or num < smallest :
smallest = num
print("small", smallest)
print("Minimum is", smallest)
print("Maximum is", largest)
The output I get just takes the first number as min and last number as min.
Enter a number: 10
Enter a number: 17
Enter a number: 20
Enter a number: 3
Enter a number: 6
Enter a number: done
Minimum is 10
Maximum is 6
I'm stumped any help would be much appreciated.
You're comparing strings, so it's doing a lexicographical order, where 6 is larger than 10. You need to convert the number strings to actual numbers before comparing:
str_num = input("Enter a number: ")
if str_num == "done":
break
num = int(str_num) # Convert to integer
I'm new in python and I always encounter this problem when looping. What should I do to make the 'print' and 'input' in one line?
Please help me ^.^
This is my code
subjects = int(input("How many subjects do you have?"))
subject_grades = []
sum = 0
for e in range(0, subjects):
print("Enter the grade of your subject number ", e+1, ": ", sep="")
grade = int(input())
subject_grades.append(grade)
sum+=grade
average= sum/len(subject_grades)
print("The sum of your grades is", sum, "and the average is", average)
I expect the output of this code to be:
How many subjects do you have?3
Enter the grade of your subject number 1: 1
Enter the grade of your subject number 2: 2
Enter the grade of your subject number 3: 3
The sum of your grades is 6 and the average is 2.0
but the actual output is:
How many subjects do you have?3
Enter the grade of your subject number 1:
1
Enter the grade of your subject number 2:
2
Enter the grade of your subject number 3:
3
The sum of your grades is 6 and the average is 2.0
Use
grade = int(input("Enter the grade of your subject number " + str(e+1) + ": "))
instead of
print("Enter the grade of your subject number ", e+1, ": ", sep="")
grade = int(input())
I have a task for my college in which I must program some very simple logic for multiplying numbers in range like 1 to 5 and than multiply like 1*2*3*4*5. And this way for any input number. For 7 it would be 1*2*3*4*5*6*7.
This is my modest code which is not finished because of no idea how to do it .Help, please..
number = int(input("Enter a number:"))
number += 1
for i in range(1,number):
a = i*(number*
print(a)
Try this:
In [1774]: number = int(input("Enter a number:"))
In [1775]: a = 1
In [1776]: for i in range(1, number+1):
...: a *= i
In [1781]: a
Out[1781]: 120
a's value is 120 which is basically (1*2*3*4*5). Hope this helps.
This code works but it's not what I need
lst = range(1,17)
It uses from 1 to 16 to do some calculations but in fact I want to specify the values, when I type them inside the code it works:
lst = (14, 1, 6, 8)
but what I want is the user who must choose which values to be in the list by inputting them
I have tried this but it does not works
lst = (a1, a2, a3)
a1 = int(input("number 1: "))
a2 = int(input("number 2: "))
a3 = int(input("number 3: "))
You have to switch the order of your lines of code:
a1 = int(input("number 1: "))
a2 = int(input("number 2: "))
a3 = int(input("number 3: "))
lst = (a1, a2, a3)
Python will execute your example code line-by-line in top-down order, so if you want to create list/tuple from inputed values the creation has to be as last one.
I'm trying to make a triangle of number like this one here
1
12
123
12
1
Number of line is chosen by the user. For example if user enters : 5. There must be 5 line (like in the previous triangle). And the number must be an odd number.
Here is what I have done so far but i'm stuck.
oddNumber = int(input("Enter an odd number here:"))
for i in range(oddNumber):
for j in range(oddNumber):
print("", end="")
for k in range(1,i):
print(k, end="")
print("")
The output of that is (if I enter 5):
1
12
123