ValueError: invalid literal for int() with base 10: '-f'
ValueError: invalid literal for int() with base 10: '-f'
这是一个关于如何在 Jupyter notebook 中使用命令行 运行 仅通过文件名和一些输入的代码的示例 ipython RollDie.py 600
。
我自己编写了代码并采用了在线示例并尝试 运行 两者,但它一直给我同样的错误 ValueError: invalid literal for int() with base 10: '-f'
。错误在第 9 行,因为 int(sys.argv[1]))
我试图将其转换为 float 然后 int 但它只是说 could not convert string to float: '-f'
.
我为我的案例搜索了很多解决方案,但没有找到类似这样的方法,只是找到了一种方法,我应该编写一种方法来读取文件(顺便说一句,我是处理文件编码的新手),但是book 在他的例子中它想在编写代码后创建文件并且没有提到任何关于这个错误的信息。
注意:argv[0]
是字符串 'RollDie.py'
而 argv[1]
应该是输入字符串 '600'
所以将 argv[1]
转换为 int 应该没有问题。
书名是“Python for Programmers”。
代码:
"""Graphing frequencies of die rolls with Seaborn."""
import matplotlib.pyplot as plt
import numpy as np
import random
import seaborn as sns
import sys
# use list comprehension to create a list of rolls of a six-sided die
rolls = [random.randrange(1, 7) for i in range(int(float(sys.argv[1])))] # Range is written that way so we can modify the code with the command line directly and no need to rund the code by yourself(A pro gamer move).
# NumPy unique function returns unique faces and frequency of each face
values, frequencies = np.unique(rolls, return_counts=True)
title = f'Rolling a Six-Sided Die {len(rolls):,} Times'
sns.set_style('whitegrid') # white backround with gray grid lines
axes = sns.barplot(values, frequencies, palette='bright') # create bars
axes.set_title(title) # set graph title
axes.set(xlabel='Die Value', ylabel='Frequency') # label the axes
# scale y-axis by 10% to make room for text above bars
axes.set_ylim(top=max(frequencies) * 1.10)
# display frequency & percentage above each patch (bar)
for bar, frequency in zip(axes.patches, frequencies):
text_x = bar.get_x() + bar.get_width() / 2.0
text_y = bar.get_height()
text = f'{frequency:,}\n{frequency / len(rolls):.3%}'
axes.text(text_x, text_y, text,
fontsize=11, ha='center', va='bottom')
plt.show() # display graph
%save RollDie.py 1
似乎 -f
是 sys.argv[1] 而你是 运行 代码,这就是你得到错误的原因(当然,你可以'将 -f
转换为浮点数或字符串)。当您 运行 文件时,您是否明确说明了这一点?不确定,例如:
ipython RollDie.py -f 600
如果不是,则可能与某种未显示的内置命令有关。您是否尝试过使用 python 而不是 ipython?
这是一个关于如何在 Jupyter notebook 中使用命令行 运行 仅通过文件名和一些输入的代码的示例 ipython RollDie.py 600
。
我自己编写了代码并采用了在线示例并尝试 运行 两者,但它一直给我同样的错误 ValueError: invalid literal for int() with base 10: '-f'
。错误在第 9 行,因为 int(sys.argv[1]))
我试图将其转换为 float 然后 int 但它只是说 could not convert string to float: '-f'
.
我为我的案例搜索了很多解决方案,但没有找到类似这样的方法,只是找到了一种方法,我应该编写一种方法来读取文件(顺便说一句,我是处理文件编码的新手),但是book 在他的例子中它想在编写代码后创建文件并且没有提到任何关于这个错误的信息。
注意:argv[0]
是字符串 'RollDie.py'
而 argv[1]
应该是输入字符串 '600'
所以将 argv[1]
转换为 int 应该没有问题。
书名是“Python for Programmers”。
代码:
"""Graphing frequencies of die rolls with Seaborn."""
import matplotlib.pyplot as plt
import numpy as np
import random
import seaborn as sns
import sys
# use list comprehension to create a list of rolls of a six-sided die
rolls = [random.randrange(1, 7) for i in range(int(float(sys.argv[1])))] # Range is written that way so we can modify the code with the command line directly and no need to rund the code by yourself(A pro gamer move).
# NumPy unique function returns unique faces and frequency of each face
values, frequencies = np.unique(rolls, return_counts=True)
title = f'Rolling a Six-Sided Die {len(rolls):,} Times'
sns.set_style('whitegrid') # white backround with gray grid lines
axes = sns.barplot(values, frequencies, palette='bright') # create bars
axes.set_title(title) # set graph title
axes.set(xlabel='Die Value', ylabel='Frequency') # label the axes
# scale y-axis by 10% to make room for text above bars
axes.set_ylim(top=max(frequencies) * 1.10)
# display frequency & percentage above each patch (bar)
for bar, frequency in zip(axes.patches, frequencies):
text_x = bar.get_x() + bar.get_width() / 2.0
text_y = bar.get_height()
text = f'{frequency:,}\n{frequency / len(rolls):.3%}'
axes.text(text_x, text_y, text,
fontsize=11, ha='center', va='bottom')
plt.show() # display graph
%save RollDie.py 1
似乎 -f
是 sys.argv[1] 而你是 运行 代码,这就是你得到错误的原因(当然,你可以'将 -f
转换为浮点数或字符串)。当您 运行 文件时,您是否明确说明了这一点?不确定,例如:
ipython RollDie.py -f 600
如果不是,则可能与某种未显示的内置命令有关。您是否尝试过使用 python 而不是 ipython?