如何从字符串中获取数字然后乘以数字?

时间:2016-03-22 11:20:25

标签: python

我目前无法从数字中获取数字并乘以数字。这是我的代码:

Ask for the users INPUT
do they want to check an 8-digit number or generate a check digit from a 7-digit number?
print("Welcome to the Barcode Scanner. There are two options: ")
print("Do you want to check an 8-digit or generate a check digit from a 7 digit number?")
usersDecision = input("Press 7 to generate the 7th digit, or press 8 to check an 8 digit number")
Get the user to INPUT their 7 or 8 digit number
if usersDecision == "7":
print("You have chosen to generate a check digit from a 7 digit number?")
seven_digitCheck = input("Please enter your 7-digit number")

elif usersDecision == "8":
print("You have chosen to check the validity of an 8 digit number.")
eight_digitValid = input ("Please enter your 8 digit number")

else:
print("This is not a valid option, Please leave")
Get the first digit and MULTIPLY by 3 (this applies to both 7 and 8 digits)

2 个答案:

答案 0 :(得分:0)

eight_digitValidseven_digitCheck是一个字符串。因此,提取第一个数字,将其转换为int,然后乘以

first_digit = eight_digitValid [0]
numb = int(first_digit ) * 3

答案 1 :(得分:0)

如果我理解正确,您需要以下解决方案:

print("Welcome to the Barcode Scanner. There are two options: ")
print("Do you want to check an 8-digit or generate a check digit from a 7 digit number?")
usersDecision = input("Press 7 to generate the 7th digit, or press 8 to check an 8 digit number")

if usersDecision == "7":
    print("You have chosen to generate a check digit from a 7 digit number?")
    seven_digitCheck = input("Please enter your 7-digit number")

elif usersDecision == "8":
    print("You have chosen to check the validity of an 8 digit number.")
    eight_digitValid = input ("Please enter your 8 digit number")

else:
    print("This is not a valid option, Please leave")

multiply_1 = seven_digitCheck * 3

multiply_2 = eight_digitValid * 3

print "multiply_1: " + repr(multiply_1) + "\n"
print "multiply_2: " + repr(multiply_2) + "\n"