我编写了一个功能,可以输入多个用户并使用输入的数据。以下函数位于file1
def dimensions
f = open("file.txt", "a")
distance= (raw_input("Enter distance: "))
height= raw_input("Enter height: "))
length= raw_input("Enter length: "))
f.write(fullInfo)
f.close()
以下内容属于file2
。 File2
使用file1
调用import
此函数dimensions
的调用如下:
def calculation():
print "1- Enter dimensions"
print "2- Enter something else"
choice = raw_input("")
if choice == 1:
file1.dimensions
我遇到的问题是,如果用户不想输入维度,并且错误地在菜单中输入了1
,他仍然需要输入所有三个查询。
我尝试在file1
中导入file2
并编写以下代码,以便它可以返回菜单,但是将两个文件相互导入似乎无法正常工作。这是在file1
if distance == 'exit':
file2.calculation
任何有关如何让用户脱离问题并返回菜单的帮助或指示将不胜感激。感谢
答案 0 :(得分:0)
添加一个名为option 0的附加选项,这意味着用户想要重新启动并添加if条件来检查用户是否输入了0
int N = int.Parse(Console.ReadLine());
String line = Console.ReadLine();
int[] A = line
.Split(new Char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Select(item => int.Parse(item))
.ToArray();
// given N differs from actual one
if (N != A.Length) {
// Throw an exception or re-assign N
N = A.Length;
}
// A as well as N are obtained
for (j = 0; j < N; j++)
if A[j] % 2 != 0
CUL++;
else
cL++;
if (cL > CUL)
Console.WriteLine("READY FOR BATTLE");
else
Console.WriteLine("NOT READY");
答案 1 :(得分:0)
fullInfo
未在您发布的代码中定义。if choice == 1:
永远不会成立,因为raw_input("")
/ choice
是一个字符串,需要强制转换为int
才能与1进行比较才有意义。 在下面的代码段中,dimensions
缺少括号和冒号,distance
不应嵌套/缩进f = open("file.txt", "a")
def dimensions
f = open("file.txt", "a")
distance= (raw_input("Enter distance: "))
除非添加括号,否则您实际上调用函数。这适用于以下calculation
:
if distance == 'exit':
file2.calculation
&#34;(...)将两个文件相互导入(...)&#34; 称为循环导入或循环导入。有办法让它发挥作用,但如果可能的话,最好避免使用它。如果您有兴趣,可以参考here。
是的,对不起......我很无聊
<强> menu.py 强>
import json
import os
import get_dimensions
def dims_load(dims_json_file):
""" you hand this function a filename and it returns a dictionary of dimensions """
with open(dims_json_file, "r") as dims_json:
return json.loads(dims_json.read())
def dims_save(dims_dict, dims_json_file):
""" you hand this function a dictionary and a filename to save the dimensions """
with open(dims_json_file, "w") as dims_json:
dims_json.write(json.dumps(dims_dict))
def menu():
dimensions_file = "dimensions.json"
file_exists = os.path.isfile(dimensions_file)
print "1- Enter dimensions"
print "2- Load previous dimensions"
print "3- Johny's funky function!"
choice = raw_input()
try:
choice = int(choice)
except ValueError as e:
print "Only numbers can be entered. Error details:", str(e)
if choice == 1:
dims_dict = get_dimensions.prompt_user()
if "exit" in dims_dict.values():
print "User chose to exit get_dimensions()"
menu()
else:
if file_exists:
print "overwriting your precious data!"
dims_save(dims_dict, dimensions_file)
elif choice == 2:
if file_exists:
print dims_load(dimensions_file)
else:
print "No dimensions file found. Can't print"
elif choice == 3:
print "Johny's funky function has yet to see the light of day..."
else:
print "I have no idea what you want. Make up your mind, human!?"
menu()
<强> get_dimensions.py 强>
def prompt_user():
dims = {
"distance":"",
"length":"",
"height":""
}
dims_keys = dims.keys()
current = 0
while current < len(dims_keys):
dims[dims_keys[current]] = raw_input("Enter " + dims_keys[current] + " (type 'exit' to return to menu): ")
current += 1
if "exit" in dims.values():
return dims
return dims