I type in seven digits to be able to work out a gtin-8 product code. But if I type in more than seven digits, the if len statement is meant to recognise that I have typed in more than seven digits, but it doesn't. I tried putting this into an variable but that did not work either... Any help would be appreciated!!! This is my code........
gtin1 = int(input("Enter your first digit... "))
gtin2 = int(input("Enter your second digit... "))
gtin3 = int(input("Enter your third digit... "))
gtin4 = int(input("Enter your fourth digit... "))
gtin5 = int(input("Enter your fifth digit... "))
gtin6 = int(input("Enter your sixth digit... "))
gtin7 = int(input("Enter your seventh digit... "))
gtin_join = (gtin1, gtin2, gtin3, gtin4, gtin5, gtin6, gtin7)
if len(gtin_join) == 7:
What you probably want to do is something like that (notice that I'm using a list here):
ls = []
while len(ls) < 7:
try: #always check the input
num = int(input("Enter your {0} digit:".format(len(ls)+1) ))
ls.append(num)
except:
print("Input couldn't be converted!")
print(ls) #ls now has 7 elements
Your created tuple always has a length of 7 so your if-statement always turns out to be True.
For the difference between list and tuple please see this question here.
Your gtin_join is tuple, and if you want list you should use square braces. You can test type of variables with this example:
gtin1 = int(input("Enter your first digit... "))
gtin2 = int(input("Enter your second digit... "))
gtin3 = int(input("Enter your third digit... "))
gtin4 = int(input("Enter your fourth digit... "))
gtin5 = int(input("Enter your fifth digit... "))
gtin6 = int(input("Enter your sixth digit... "))
gtin7 = int(input("Enter your seventh digit... "))
gtin_join = (gtin1, gtin2, gtin3, gtin4, gtin5, gtin6, gtin7)
print(type(gtin_join))
gtin_join = [gtin1, gtin2, gtin3, gtin4, gtin5, gtin6, gtin7]
print(type(gtin_join))
if len(gtin_join) == 7:
print 7
I would do the following:
gtin_list = []
while len(gtin_list) != 7:
gtin = input("Please enter all 7 digits separated by commas...")
gtin_list = [int(x) for x in gtin.split(",")]
Related
So, the homework is: I need to write a code that will let the user to enter 3 numbers(this part is done.). Then my code should compare those numbers with each other(I think I know this too). But the hardest part is: if the first num is greater then code should print 1st: true, if the second one is greater it should print 2nd: true and so on. Also I can't use strings, if else and others the only thing I can use are operators,variables, input and typecasting.
I came up with this idea:
first = int(input('Write first number: '))
second = int(input('Write second number: '))
third = int(input ('Write third number: '))
print (f'1st: {first > second and first> third}')
print (f'2nd: {second > first and second > third}')
print (f'3rd: { third > first and third > second}')
use the code below:
first = int(input('Write first number: '))
second = int(input('Write second number: '))
third = int(input ('Write third number: '))
temp=first > second and first> third and print("1st:True")
temp=second > first and second > third and print("2st:True")
temp=third > first and third > second and print("3st:True")
have fun :)
Looks like a pretty specific and restrictive application. In your case, one could go with a ternary operator to eliminate the if else statement:
(false_value, true_value)[conditional_expression]
In the example below, I am assuming you would want to output False in the case that the conditions are not met. If this is not the case, I would suggest leaving it a blank string, i.e ""
first = int(input('Write first number: '))
second = int(input('Write second number: '))
third = int(input('Write third number: '))
first_result = ("1st: False", "1st: True")[(first > second) and (first > third)]
second_result = ("2nd: False", "2nd: True")[(second > first) and (second > third)]
third_result = ("3rd: False", "3rd: True")[(third > first) and (third > second)]
print(first_result)
print(second_result)
print(third_result)
first = int(input('Write first number: '))
second = int(input('Write second number: '))
third = int(input('Write third number: '))
first_result = ("1st: False", "1st: True")[(first > second) and (first > third)]
second_result = ("2nd: False", "2nd: True")[(second > first) and (second > third)]
third_result = ("3rd: False", "3rd: True")[(third > first) and (third > second)]
print(first_result)
print(second_result)
print(third_result)
I need to write a program that asks the user to enter a number, n, which is -6
The numbers needed to be printed using a field width of 2 and need to be right justified. Fields must be seperated by a single space, with no space after the final field.
so far this is what i have got to this:
n = int(input("Enter the start number: "))
if n>-6 and n<93:
for i in range(n, n+7):
print("{:>2}".format(i), end=" ")
But I still seem to be getting spacing issues, any help would be much appreciated!
n = int(input("Enter the start number: "))
if n>-6 and n<93:
for i in range(n, n+7):
if i < n + 6:
print("{:>2}".format(i), end=" ")
else:
print("{:>2}".format(i))
Solution
n = int(input("Enter the start number: "))
width = int(input('Enter width : '))
if n>-6 and n<93:
for i,val in enumerate(range(n,n+width)):
if i==(width-1):
print("{}".format(val))
else:
print("{}".format(val),
end=" ")
Output
Enter the start number: 5
Enter width : 5
5 6 7 8 9
I am trying to write a program which asks an input of two numbers and then prints the sum, product and average by running it. I wrote a program but it asks an input for 2 numbers everytime i need a sum or average or product. How can I get all 3 at once, just making two inputs once.
sum = int(input("Please enter the first Value: ")) + \
int(input("Please enter another number: "))
print("sum = {}".format(sum))
product = int(input("Please enter the first Value: ")) * \
int(input("Please enter another number: "))
print ("product = {}".format(product))
Use variables to store the input:
first_number = int(input("Please enter the first Value: "))
second_number = int(input("Please enter another number: "))
sum = first_number + second_number
product = first_number * second_number
average = (first_number + second_number) / 2
print('Sum is {}'.format(sum))
print('product is {}'.format(product))
print('average is {}'.format(average))
You need to assign your numbers to variables and then reuse them for the operations.
Example
x = int(input("Please enter the first Value: "))
y = int(input("Please enter another number: "))
print("sum = {}".format(x+y))
print("product = {}".format(x*y))
print("average = {}".format((x+y)/2))
You're going to want to get the numbers first, then do your operation on them. Otherwise you're relying on the user to alway input the same two numbers:
a = int(input("Please enter the first Value: "))
b = int(input("Please enter the second Value: "))
print ("sum = {}".format(a+b))
print ("product = {}".format(a*b))
print ("average = {}".format((a*b)/2))
I have this code for a program that should manipulate certain inputs the user enters.
I'm not sure how to only get x number of ouputs (x is specified by the user at the start of the program).
numOfFloats = int(input("Enter the number of floating point inputs: "))
numOfInts = int(input("Enter the number of integer inputs: "))
numOfStrings = int(input("Enter the number of string inputs: "))
for num in range(numOfStrings,0,-1):
print()
ffloats = float(input("Enter a real number: "))
iints = int(input("Enter an integer: "))
string = input("Enter a string: ")
print()
print("float: ", ffloats**(1/10))
print("int: ", iints**10)
print("string: ", (string + string))
I get all three requests each time, even though I have specified in the beginning that I only want 1 float, 2 ints, and 3 strings. I get asked for 3 floats, 3 ints, and 3 strings. I do realize what my code does, but I'm not sure how to get it to where I want it. I have a feeling something is wrong in the for loop conditions.
Any help is appreciated!
ffloats = []
for num in range(numOfFloats):
ffloats.append(float(input("\nEnter a real number: "))
iints = []
for num in range(numOfFloats):
iints.append(int(input("\nEnter an integer: "))
sstrings = []
for num in range(numOfFloats):
sstrings.append(input("\nEnter a real number: ")
print("Floats:", [f**(1/10) for f in ffloats])
print("Ints:", [i**10 for i in iints])
print("Strings:", [s + s for s in sstrings])
If you want them in order, then you'll have to:
for v in range(max([numOfFloats, numOfInts, numOfStrings])):
if v < numOfFloats:
ffloats.append(float(input("\nEnter a real number: "))
if v < numOfInts:
iints.append(int(input("\nEnter an integer: "))
if v < numOfStrings:
sstrings.append(input("\nEnter a string: ")
The program did exactly what you told it to do: given the number of strings -- 3 -- get that many int-float-string sets. You never used the other two quantities to control their loops. You need three separate loops; here's the one for strings, with all the int and float stuff removed.
numOfStrings = int(input("Enter the number of string inputs: "))
for num in range(numOfStrings,0,-1):
print()
string = input("Enter a string: ")
print()
print("string: ", (string + string))
Now just do likewise for ints and floats, and I think you'll have what you want.
Yes, you can do it in one loop, but it's inelegant. You have to find the max of all three numbers and use that as the loop's upper limit. Within the loop, check "num" against the int, float, and string limits, each in turn.
This code would be less readable, harder to maintain, and slower. Do you have some personal vendetta against loops? :-)
If your really want just a single loop, then I would suggest you use a while loop rather than a for loop, as you need to keep looping until all values have been entered.
numOfFloats = int(input("Enter the number of floating point inputs: "))
numOfInts = int(input("Enter the number of integer inputs: "))
numOfStrings = int(input("Enter the number of string inputs: "))
while numOfFloats + numOfInts + numOfStrings:
print()
if numOfFloats:
ffloats = float(input("Enter a real number: "))
if numOfInts:
iints = int(input("Enter an integer: "))
if numOfStrings:
string = input("Enter a string: ")
print()
if numOfFloats:
print("float: ", ffloats**(1/10))
numOfFloats -= 1
if numOfInts:
print("int: ", iints**10)
numOfInts -= 1
if numOfStrings:
print("string: ", (string + string))
numOfStrings -= 1
So for example:
Enter the number of floating point inputs: 1
Enter the number of integer inputs: 2
Enter the number of string inputs: 3
Enter a real number: 1.5
Enter an integer: 2
Enter a string: three
float: 1.0413797439924106
int: 1024
string: threethree
Enter an integer: 2
Enter a string: hello
int: 1024
string: hellohello
Enter a string: world
string: worldworld
Question: Create a program that allows the user to enter 10 different integers. If the user tries to enter
an integer that has already been entered, the program will alert the user immediately and
prompt the user to enter another integer. When 10 different integers have been entered,
the average of these 10 integers is displayed.
This is my code:
mylist = []
number = int(input("Enter value: "))
mylist.append(number)
while len(mylist) != 10:
number = int(input("Enter value: "))
if number in mylist:
number = int(input("The number is already in the list, enter another number: "))
mylist.append(number)
else:
mylist.append(number)
print(sum(mylist)/float(len(mylist)))
This kind of works but I need to create a loop that will keep on asking the user for another number if the number is in the array. Can you help?
What about:
mylist = []
number = int(input("Enter value: ")) mylist.append(number)
while len(mylist) != 10:
number = int(input("Enter value: "))
while number in mylist:
number = int(input("The number is already in the list, enter another number: "))
mylist.append(number)
print(sum(mylist)/float(len(mylist)))