如何将.in文件作为python的输入
这是我的密码如何将.in文件作为python的输入,python,input,Python,Input,这是我的密码 def jumlah(A,B,C): global result result = A+B+C count = 0 i = eval(input('input total test case: ')) while count < i : A = eval(input('input A: ')) B = eval(input('input B: ')) C = eval(input('input C: ')) jumlah(
def jumlah(A,B,C):
global result
result = A+B+C
count = 0
i = eval(input('input total test case: '))
while count < i :
A = eval(input('input A: '))
B = eval(input('input B: '))
C = eval(input('input C: '))
jumlah(A,B,C)
count = count + 1
print('case no'+str(count)+' : '+str(result))
第一行是案例总数,其余的是A、B和C的输入。我的预期结果是
case no1 : 6
case no2 : 9
请帮忙。谢谢您只需打开输入文件并阅读
with open("input.in", "r") as inputs:
for line in ins:
#your inputs one by one.
您可以打开输入文件并读取它
with open("input.in", "r") as inputs:
for line in ins:
#your inputs one by one.
您应该将代码分解为单独的函数,这些函数执行您想要执行的操作。在这种情况下,如果用户希望读取文件或手动输入,则可以提示用户。根据该决定,您可以调用相应的函数
def jumlah(A,B,C):
result = A+B+C
return result
def start():
option = input(' Would you like to: \n'
' - (r) read from a file \n'
' - (i) input(i) by hand \n'
' - (q) quit \n ')
if option.lower() not in 'riq':
print('Invalid choice, please select r, i, or q.')
option = start()
return option.lower()
def by_hand():
count = 0
i = eval(input('input total test case: '))
while count < i :
A = eval(input('input A: '))
B = eval(input('input B: '))
C = eval(input('input C: '))
result = jumlah(A,B,C)
count = count + 1
print('case no'+str(count)+' : '+str(result))
def from_file():
path = input('Please input the path to the file: ')
with open(path, 'r') as fp:
cases = int(fp.readline().strip())
for i in range(1, cases+1):
a,b,c = fp.readline(), fp.readline(), fp.readline()
result = jumlah(A,B,C)
print('case no'+str(i)+' : '+str(result))
def main():
while True:
opt = start()
if opt == 'r':
from_file()
if opt == 'i':
by_hand()
if opt == 'q':
print('Goodbye.')
return
if __name__ == '__main__':
main()
def jumlah(A、B、C):
结果=A+B+C
返回结果
def start():
选项=输入('您想:\n'
“-(r)从文件中读取\n”
'-(i)手动输入(i)\n'
“-(q)退出\n”)
如果option.lower()不在“riq”中:
打印('选择无效,请选择r、i或q'。)
选项=开始()
返回选项。下()
手动定义():
计数=0
i=eval(输入('输入总测试用例:'))
当计数
您应该将代码分解为单独的函数,这些函数执行您想要执行的操作。在这种情况下,如果用户希望读取文件或手动输入,则可以提示用户。根据该决定,您可以调用相应的函数
def jumlah(A,B,C):
result = A+B+C
return result
def start():
option = input(' Would you like to: \n'
' - (r) read from a file \n'
' - (i) input(i) by hand \n'
' - (q) quit \n ')
if option.lower() not in 'riq':
print('Invalid choice, please select r, i, or q.')
option = start()
return option.lower()
def by_hand():
count = 0
i = eval(input('input total test case: '))
while count < i :
A = eval(input('input A: '))
B = eval(input('input B: '))
C = eval(input('input C: '))
result = jumlah(A,B,C)
count = count + 1
print('case no'+str(count)+' : '+str(result))
def from_file():
path = input('Please input the path to the file: ')
with open(path, 'r') as fp:
cases = int(fp.readline().strip())
for i in range(1, cases+1):
a,b,c = fp.readline(), fp.readline(), fp.readline()
result = jumlah(A,B,C)
print('case no'+str(i)+' : '+str(result))
def main():
while True:
opt = start()
if opt == 'r':
from_file()
if opt == 'i':
by_hand()
if opt == 'q':
print('Goodbye.')
return
if __name__ == '__main__':
main()
def jumlah(A、B、C):
结果=A+B+C
返回结果
def start():
选项=输入('您想:\n'
“-(r)从文件中读取\n”
'-(i)手动输入(i)\n'
“-(q)退出\n”)
如果option.lower()不在“riq”中:
打印('选择无效,请选择r、i或q'。)
选项=开始()
返回选项。下()
手动定义():
计数=0
i=eval(输入('输入总测试用例:'))
当计数
$python my\u file.py
我想可以:)$python my\u file.py
我想可以这样做:)你不应该根据一般的经验来计算用户输入。我想把我的输入转换成int,我只是四处搜索,找到了eval(input()),你知道如何不用计算就可以这样做吗?谢谢
int(input(…)
。int(input(…)不起作用,它返回的错误只能连接str(而不是“int”)对于stry,你不应该将用户输入作为一般规则进行求值。我想将我的输入转换为int,我只是四处搜索并找到了eval(input()),你知道如何在不进行求值的情况下完成吗?谢谢你,int(input(…)。int(input(…)不起作用,它返回的错误只能连接str(而不是“int”)第一行应该是案例总数,所以它不应该包含在函数中。第一行应该是案例总数,所以它不应该包含在函数中。哇,这确实有效,非常感谢。真的很感谢你的帮助。哇,这真的很有效,非常感谢。非常感谢你的帮助。