如何解决整数除法时的语法错误
#dividing integers
first = int (input('What is your first integer '))
second = int (input('What is your second integer '))
quotient = int (input('State the quotient of',str(first),'divided by',str(second))
print('you said the quotient is ',quotient)
if quotient== first / second:
print('you are correct')
else:
print('you are incorrect')
print ( 'the quotient is ',first / second)
解决方法
你有两个问题。一个是缺失的 )
。另一个是 input()
需要单个 str
参数:
#dividing integers
first = int (input('What is your first integer '))
second = int (input('What is your second integer '))
quotient = int (input('State the quotient of '+ str(first) + ' divided by ' + str(second)))
print('you said the quotient is ',quotient)
if quotient== first / second:
print('you are correct')
else:
print('you are incorrect')
,
您的代码存在一些问题:
-
您在商的输入语句中缺少
)
。你
需要解决这个问题。 -
您的输入语句使用逗号。您应该改为使用 + 来连接输入语句中的字符串。
'说明' + str(first) + ' 除以' + str(second) 的商
-
带有
if quotient== first / second:
的代码正在检查带有浮点值的整数值。
例如,如果问题是 State the quotient of 3 divided by 2
而用户回答的是 1
,则您的 if 语句将检查如下:
if 1 == 3 / 2:
这将是永远不正确的 if 1 == 1.5
。
您需要将等式转换为
if quotient== first // second:
或 if quotient== int(first / second):
cp node_modules/laravel-mix/setup/webpack.mix.js ./
-
这里你缺少最后的 ')'
-
您正在错误地进行字符串连接。这是正确的:
quotient = int (input('State the quotient of ' + str(first) + ' 除以 '+ str(second)))
您的代码中存在一些错误。这是其中一种工作方法。
#dividing integers
first = input('What is your first integer ')
second = input('What is your second integer ')
quotient = input(f'State the quotient of {first} divided by {second}')
print('you said the quotient is ',quotient)
if float(quotient) == int(first) / int(second):
print('you are correct')
else:
print('you are incorrect')
print ( 'the quotient is ',int(first) / int(second))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。