0

I'm working on a game in Python (for fun). However, I've come into a few issues where the game looks for a page to return the string "1", but the actual return is "1\r\n". This fouls my response checking. Is there any way to handle both strings?

Code:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# enable debugging
import cgitb
cgitb.enable()

print("Content-Type: text/plain;charset=utf-8")
print()

print(1)
Prune
  • 76,765
  • 14
  • 60
  • 81

1 Answers1

0

You have several options, depending upon the range of inputs you expect.

The first, as @ryugie already gave you, is to strip whitespace off the string:

text = text.strip()

If you have single-character input, you can simply grab the first character:

text = text[0]

If you want to go directly to the integer value, you can directly convert the string; whitespace is ignored:

in_value = int(text)

Do those fit your needs?

Prune
  • 76,765
  • 14
  • 60
  • 81
  • Ah thanks, didn't realise it was that simple! Though it'd be something more to do with the web server, thanks again :) –  Mar 21 '17 at 19:54