0

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.

FictionFighter
  • 97
  • 1
  • 1
  • 6

3 Answers3

1

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

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Hey, yeah i've done some others scripts so I'm pretty sure I know how to use it, what im unsure about is how to in corporate it in this script I already have. If it's the maxnra and b or the x and y that needs to be user inputs. – FictionFighter Oct 21 '11 at 12:52
1

If you just want a simple way to get user input from the terminal window, have a look at the raw_input function.

imm
  • 5,837
  • 1
  • 26
  • 32
0

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

Community
  • 1
  • 1
Surya
  • 4,824
  • 6
  • 38
  • 63