Python - 需要建议
下面是我在尝试完成一项作业时完成的脚本。
该脚本要做的就是要求用户提供 2 个输入,然后返回较大的输入。 (这个我还没有完全弄清楚)
这个作业的重点是看看如果我不输入 2 个数字,而是输入两个单词“Hej”和“Hå”会发生什么。
我需要一些建议,如何让这个脚本接受 2 个用户输入并返回其中较大的一个。
def maximum(x, y):
i = 0
maxnra = 0
maxnrb = 0
while i < len(x) :
if x[i] > maxnra:
maxnra = x[i]
i = i + 1
else:
i = i + 1
print "I första ordet är maximum: ", maxnra
i = 0
while i < len(y) :
if y[i] > maxnrb:
maxnrb = y[i]
i = i + 1
else:
i = i + 1
print "I andra ordet är maximum: ", maxnrb
maximum("hej", "hå")
编辑:
我尝试用另一种方式解决这个问题,这是解决这个问题的方法吗?
print "First"
x = input()
print "Second"
y = input()
def printMax(x, y):
if x > y:
print(x, 'is maximum')
elif a == b:
print(x, 'is equal to', y)
else:
print(y, 'is maximum')
现在我错过了一些东西,因为当我输入 2 个值时它没有返回任何内容。
Below I have a script that I have done while trying to complete for a assignment I have.
What the script is suppose to do is ask the user for 2 inputs and then return the greater of the inputs. (This I havent figured out completely yet)
The point of this assignment is too see what happens if I instead of entering 2 numbers, enter two words "Hej" and "Hå".
What I need some advice on is how to enable this script to accept 2 user inputs and return the greater of them two.
def maximum(x, y):
i = 0
maxnra = 0
maxnrb = 0
while i < len(x) :
if x[i] > maxnra:
maxnra = x[i]
i = i + 1
else:
i = i + 1
print "I första ordet är maximum: ", maxnra
i = 0
while i < len(y) :
if y[i] > maxnrb:
maxnrb = y[i]
i = i + 1
else:
i = i + 1
print "I andra ordet är maximum: ", maxnrb
maximum("hej", "hå")
EDIT:
I tried working this out another way, is this a way to solving this?
print "First"
x = input()
print "Second"
y = input()
def printMax(x, y):
if x > y:
print(x, 'is maximum')
elif a == b:
print(x, 'is equal to', y)
else:
print(y, 'is maximum')
right now im missing something cause it's not returning anything when I enter the 2 values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
阅读有关命令 raw_input 的文档,了解如何获取用户的输入。
Read documention on the command raw_input to see how you can get input from the user.
如果您只是想要一种从终端窗口获取用户输入的简单方法,请查看 raw_input 函数。
If you just want a simple way to get user input from the terminal window, have a look at the raw_input function.
您的第一个代码只需使用两个列表并打印每个列表的最大值。所以,这不是你想要的。
在第二个代码中,虽然方法是正确的,但您犯了一些小错误。
实际上,当您在这段代码中输入内容时,虽然您输入的是数字,但它们仍然被视为字符串。因此,如果您输入字符串,则不会有太大区别。
使用(ASCII 值顺序)按字典顺序比较这些字符串。由于您的输入不是来自 ASCII,因此会显示错误。
因此,您需要将
input()
或raw_input(
) 替换为以下内容请阅读以下 stackoverflow 问题以了解有关此 链接
Your first code would simply takes two lists and prints the maximum value of each individual list. So, this is not that you want.
In the second code, although the approach is right, you made some minor mistakes.
Actually, when you enter input in this code, though you enter numbers they are still considered as strings. So, there would be no big difference if you enter a string.
These strings are compared lexicographically using (ASCII value order). As your input isn't from ASCII, it will show error.
So, you need to replace
input()
orraw_input(
) with the followingPlease read the following stackoverflow question to know more on this link