0

I'm trying to create a program that takes input from the user, -1 or 1 to play or quit, number of characters (n), two words for comparison (s1,s2), and calls this function: def strNcompare (s1,s2,n) and then prints the result. The function should return 0,-1,1 if n-character portion of s1 is equal to, less than, or greater than the corresponding n-character of s2, respectively. So for example is the first string is equal to the second string, it should return a 0.

The loop should end when the user enters, -1.

I'm just starting out in Python so the code I have is pretty rough. I know I need an if/else statement to print the results using the return value of the function and a while loop to make the program run.

This is what I have so far, it by no means works but my knowledge ends here. I don't know how to integrate the character (n) piece at all either.


com = input ("String comparison [1(play), -1(quit)]: ")

while (com=='1'):
 s1 = input ("enter first string: ") 
 s2 = input ("enter second string: ") 
 n = int(input("number of characters: ")
 s1 = s1[:n]
 s1 = s2[:n]

 if com==-1:
  break

def strNcompare(s1,s2,n):
 return s1==s2

 elif s1==s2:
  print(f'{s1} is equal to {s2}')
 elif s1>s2:
  print (f'{s1} is greater than {s2}')
 elif s1<s2:
  print (f'{s1} is less than {s2}')
 else: 
  print ("QUIT")
 break 

com = input ("String comparison [1(play), -1(quit)]: ")


As of 10/05/2019 - I revised the code and now I am getting a syntax error at "s1 = s1[:n]"

1 Answer 1

0

it did not made much sense and especially the variable 'n' is not completely clear to me. I would not code it like that but I tried to be as close to your logic as possible.

com = input ("String comparison [1(play), -1(quit)]: ")

while (com=='1'):
  s1 = input ("enter first string: ") 
  s2 = input ("enter second string: ") 
  n = input ("number of characters: ") #what is the purpose of this? 

  if s1==s2:
      print(f'{s1} is equal than {s2}')
  elif s1>s2:
      print (f'{s1} is greater than {s2}')
  elif s1<s2:
      print (f'{s1} is less than {s2}')
  else: 
      print ("Error")
Sign up to request clarification or add additional context in comments.

2 Comments

The n is the number of characters entered by the user. So say the user enters 3, the first three characters of each string should be compared. This is a concept I completely don’t understand.
Also the printed solution to the program, for example “s1 is greater than s2”, is created as a result of the return 0,-1,1 from the function once the comparison is made. This is just something that’s part of the program even though it’s not really necessary. That’s why I created that if/else statement with the return and tried to tie it in to another if/else statement that prints the result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.