如何从一个字符串中得到一个数,然后将这个数相乘?

2024-04-20 13:49:41 发布

您现在位置: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)

Tags: orthetofromannumberinputcheck
2条回答

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

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"

eight_digitValidseven_digitCheck是字符串。所以提取第一个数字,把它转换成int,然后相乘

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

相关问题 更多 >