Python - 需要建议

发布于 2024-12-11 17:14:16 字数 1012 浏览 0 评论 0原文

下面是我在尝试完成一项作业时完成的脚本。

该脚本要做的就是要求用户提供 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好听的两个字的网名 2024-12-18 17:14:16

阅读有关命令 raw_input 的文档,了解如何获取用户的输入。

Read documention on the command raw_input to see how you can get input from the user.

执笏见 2024-12-18 17:14:16

如果您只是想要一种从终端窗口获取用户输入的简单方法,请查看 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.

旧梦荧光笔 2024-12-18 17:14:16

您的第一个代码只需使用两个列表并打印每个列表的最大值。所以,这不是你想要的。

在第二个代码中,虽然方法是正确的,但您犯了一些小错误。

print "First"
x = input() # use raw_input() for python 2.7

print "Second"
y = input()


def printMax(x, y):
     if x > y:
        print(x, 'is maximum')
    elif x == y: 
        # not a==b
        print(x, 'is equal to', y)
    else:
        print(y, 'is maximum')

实际上,当您在这段代码中输入内容时,虽然您输入的是数字,但它们仍然被视为字符串。因此,如果您输入字符串,则不会有太大区别。

使用(ASCII 值顺序)按字典顺序比较这些字符串。由于您的输入不是来自 ASCII,因此会显示错误。

因此,您需要将 input()raw_input() 替换为以下内容

    import sys # do this at the top of program.
    x = raw_input().decode(sys.stdin.encoding)
      # similarly do it for y

请阅读以下 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.

print "First"
x = input() # use raw_input() for python 2.7

print "Second"
y = input()


def printMax(x, y):
     if x > y:
        print(x, 'is maximum')
    elif x == y: 
        # not a==b
        print(x, 'is equal to', y)
    else:
        print(y, 'is maximum')

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() or raw_input() with the following

    import sys # do this at the top of program.
    x = raw_input().decode(sys.stdin.encoding)
      # similarly do it for y

Please read the following stackoverflow question to know more on this link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文