0

I want the user to input a number Give a number : he types "10" -but... Give a number : he types "I want to type 10" i want the program to just "count" the integer. Because if he types a string the program will stop

import random
goal = random.randrange(1,10)
n = 1 
tries = 0 
name = input("Dose to onoma sou ")

print("A game in Python")
while n != 0 : 
    value = int(input("madepse poio einai to noumero:")) 
    n = abs(value - goal)
    print(value,n)
    tries = tries + 1
    if n >= 4 :
     print("den eisai koda")
    elif n > 0 and n <= 3 :
     print("eisai koda")
    else :
     print("to vrikes")
     print ("to score sou einai: ",tries)

skoros = str(tries)
score = open('score.txt', 'a')
score.write(name)
score.write(' ') 
score.write(skoros)
score.write("\n") 
score.close
3
  • FYI, score.close should be score.close(). Commented Nov 1, 2015 at 19:19
  • oh yea but i just tried and without () and it works is there any difference ?. the truth is i forgot the () but i got no error ":S Commented Nov 1, 2015 at 19:34
  • It gets no error because it is just a value. It's like have a line like 1 instead of x=1. You won't get an error but it doesn't actually do anything. Commented Nov 1, 2015 at 20:49

2 Answers 2

1

This will take any input and pull the first number out of it. \d matches any digit 0-9, and + means "one or more".

import re

while True:
    user = input('Enter a number: ')
    match = re.search(r'\d+',user)
    if match:
        value = int(match.group(0))
        break
    else:
        print("I didn't see a number in that response.")

print(value)
Sign up to request clarification or add additional context in comments.

3 Comments

so there is no way just to accept the users answer ? i want to keep the integer from his answer. "my answer is 10" i want the program to keep the 10 and not break. i want to extract the integer from his answer
i get the error module re has no attribute search (its for phython3x)
I wrote The code on Python 3. It's a standard library method.
0

Well, you could just manually loop through the string and store the position of the number using isdigit().

The following approach expects the number to be the only one in the string (multi digit allowed):

start = None
stop = None
for i in range(len(input)):
    c = input[i]
    if c.isdigit():
        if start == None:
            start = i
        stop = i
try:
    number = int(input[start:stop])
catch:
    print("invalid input")

EDIT: I guess there would be some nice and easy Regex solution, but I'll leave my hands off of it, as I am not too experienced with it...

3 Comments

what about the re module ? i just dont how the syndax of re module
@D Ace For re, see Marks answer, which I would suggest to use.
well it looks fine and i get the logic of his answer but i get an error maybe i still do something wrong

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.