我编写了以下程序。该程序应该获取具有一些化学数据的文件名以及来自用户的输入,以确定该元素是否已经是文件的元素之一。但是很遗憾,在第一步中,我收到此消息,该消息输入了文件的有效名称。我不知道问题出在哪里?!任何帮助都非常感谢。
# Determine the name of a chemical element
import re
# Read the name of a file from the user
filename = input("Enter a filename:")
try:
# Open the file
f1 = open(filename, "r")
# Read an element from the user
elem = input(
"Enter the name of the element or the number of protons (" * " to exit): "
)
while elem != "*":
try:
int_elem = int(elem)
# Search in each line for the entered input
for line in f1:
if re.findall(elem, line) != []:
# Split the line to its elements
elem_line = line.split(" ")
# Print the name of element
print("The name of element is {}".format(elem_line[0]))
# print the symbol of the elemnt
print("The symbol of element is {}".format(elem_line[1]))
# Print the number of elements
print(
"The number of protons in {} is {}".format(
elem_line[0], elem_line[2]
)
)
else:
# Display if there is no elemt with this number of protons
print("There is no element with this number of protons")
break
except:
# Serach for th element in each line of the file
for line in f1:
if re.findall(elem, line) != []:
# Split the line to its elements
elem_line = line.split(" ")
# Print the number of elements
print(
"The number of protons in {} is {}".format(
elem_line[0], elem_line[2]
)
)
elem = input("Enter the name of the lement(" * " to exit):")
except:
print(" Enter a valid name for the file ")
如果运行该程序,我将得到以下输出:
输入文件名:'chemic.txt'
输入文件的有效名称
答案 0 :(得分:2)
很难知道问题出在哪里,因为代码中发生的所有异常都被该顶级except:
块“吞噬”。
用异常处理程序包装所有程序不是一种好方法–而是在发生错误时处理错误(或不这样做!–例如,用户将得到FileNotFoundError
,这可能更合适)
类似的事情可能对您有用。
import re
filename = input("Enter a filename:")
f1 = open(filename, "r")
while True:
f1.seek(0) # We need to be at the start of the file for every user input.
# Read an element from the user
elem = input("Enter the name of the element or the number of protons (* to exit): ")
if elem == "*":
break # exit the loop
# Search in each line for the entered input
for line in f1:
if re.search(elem, line):
# Split the line to its elements
elem_line = line.split(" ")
print("The name of element is {}".format(elem_line[0]))
print("The symbol of element is {}".format(elem_line[1]))
print("The number of protons in {} is {}".format(elem_line[0], elem_line[2]))
break # found a match, exit the loop
else:
print("There is no element with this name or number of protons")
答案 1 :(得分:1)
行
from datetime import date
from random import randint
from bokeh.models import AjaxDataSource, CustomJS
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn
from bokeh.models.layouts import WidgetBox
from bokeh.plotting import show, figure
import numpy as np
from flask import Flask, jsonify, make_response, request
from bokeh.plotting import figure, show
from bokeh.models import AjaxDataSource, CustomJS
adapter1 = CustomJS(code="""
const result = {x: [], y: []}
const json = cb_data.response
console.log("Hi")
for (var key in json) {
if (json.hasOwnProperty(key)) {
result.x.push(json[key].nome);
result.y.push(json[key].codigo);
}
}
console.log(result)
return result
""")
source = AjaxDataSource(data_url='https://parallelum.com.br/fipe/api/v1/carros/marcas', adapter = adapter1)
columns = [
TableColumn(field="x", title="Nome"),
TableColumn(field="y", title="Codigo"),
]
data_table = DataTable(source=source, columns=columns, width=400, height=280)
show(data_table)
不是有效的python。您正在尝试将一个字符串乘以另一个字符串。我怀疑您是想逃避elem = input(
"Enter the name of the element or the number of protons (" * " to exit): "
)
周围的"
(带有\"
)还是改用*
。
引发的'
被您的catch-everything块捕获,并且该消息假定错误来自其他地方。正如其他人所说,理想情况下,您只希望包装您希望引发异常的位,然后只捕获您期望的特定类型。