0

I'm new at stackoverflow and learning python now. I have a question and searched for it everywhere but couldn't find anything suits my idea.

So the problem is I want to check if an input (or variable) is a letter or a word or contains any letters. For an example, if the variable or respond of input is a it will return False but if the respond or variable is made of just numbers it will respond True.

I really am new in python and don't know so much commands so if there is an existing command (Or tag) which is doing just my work pls don't blame me! Thx by now and here is the code I wanted to test if the input is a letter because it was going to give an error if a letter put in.

import random
import time
repeat= True
none="none"
shutdowntime=3
repeatshut=True
def roll(count):
  print ">Rolls are these:"
  while count>0:
    print random.randint(1,6)
    count-=1  
while repeat==True:
  count=input("=>How many rolls do you want to roll?")
  if count!=none:
    roll(count)
  else:
    repeat=False

if repeat==False:
  print "=>Thank you for using my dice roll."
  while repeat==False and repeatshut==True:
    print "%d seconds to shut down." % (shutdowntime)
    shutdowntime-=1
    time.sleep(1)
    if shutdowntime==0:
      repeatshut=False
      print "Shutting down..."
      time.sleep(2)
      print "Bye!"
Elvez The Elf
  • 97
  • 1
  • 10
  • You can use : .isdigit(input), https://www.tutorialspoint.com/python/string_isdigit.htm – Rohi Apr 29 '18 at 12:52
  • 1
    Welcome to Stack Overflow! It would have been *excellent* if you had read the introductory [Tour] which was suggested when signing up – it is a short overview of how the site works. Also, pay a visit to the [Help] and browse through its topics to get an idea of common questions about questions, answers, and the site itself. – Jongware Apr 29 '18 at 12:58
  • You are asking about identifying "words" - your code needs "ints" from your inputs. Have a look at [how-do-i-parse-a-string-to-a-float-or-int-in-python](https://stackoverflow.com/questions/379906/how-do-i-parse-a-string-to-a-float-or-int-in-python) to get the latter answered and/or edit your questions to needed details why you want words if that other post does not solve your problem. – Patrick Artner Apr 29 '18 at 13:00
  • How does your current code fail? Your input, the current (wrong) output, and the expected output are part of a good question. Don't use comments to add information, you can always [edit] your own question. – Jongware Apr 29 '18 at 13:01
  • Possible duplicate of [How do I parse a string to a float or int in Python?](https://stackoverflow.com/questions/379906/how-do-i-parse-a-string-to-a-float-or-int-in-python) – Patrick Artner Apr 29 '18 at 13:01
  • 2
    This: [asking-the-user-for-input-until-they-give-a-valid-response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) might also be relevant – Patrick Artner Apr 29 '18 at 13:03
  • 1
    BTW, you should _seriously_ consider learning Python 3, Python 2 will reach its official End Of Life in 2020. But in the mean time, please do **not** use Python 2's `input` function, it is a big security hole. Use `raw_input` instead. In Python 3, `raw_input` has been renamed to `input`, and the old `input` function has been killed. – PM 2Ring Apr 29 '18 at 13:10
  • Instead of using none="none" you can use none=0 to get out of the loop and use try and except statements for getting valid user input. – Aniket Bote Apr 29 '18 at 13:15
  • Thanks to all who has replied I read all of them and decided the guide asking-the-user-for-input-until-they-give-a-valid-response is the best I think. – Elvez The Elf Apr 29 '18 at 13:33
  • Despite learning Python 2, I really consider learning 3 is better. But I want to learn Javascript just after python and it seems Python 2 is more likely to the Javascript. – Elvez The Elf Apr 29 '18 at 13:35

2 Answers2

1

You can use the built-in function for strings: isdigit().

Here you can find the documentation: https://docs.python.org/3/library/stdtypes.html#str.isdigit

toom501
  • 324
  • 1
  • 3
  • 15
  • Interesting – for common purposes this will work, but "This covers digits which cannot be used to form numbers in base 10, like the Kharosthi numbers" suggests that it used actually the *Unicode* classification of the input string. Which means it will accept `Ⅻ`. I think I'm going to try and see what happens. – Jongware Apr 29 '18 at 13:12
  • Okay – testing against selected characters from [Numerals in Unicode](https://en.wikipedia.org/wiki/Numerals_in_Unicode) shows ... mixed results. `int('६')` works (it is the Devanagari numeral '6'), but `int('①')` fails, even though this function *says* it is a valid digit. (Alas: `¾` fails the `isdigit` test, and so does `Ⅷ`. The first *may* make sense but for the latter, even if the combo fails, the singular `Ⅴ` is a "digit" in Roman numerals and it also fails.) The official documentation may need expanding on what's valid and what's not. – Jongware Apr 29 '18 at 13:58
  • 1
    I agree with you... this function is working only for general purposes. It works only with positive integers; not float! If you try `"-1".isdigit()`and `"1.1".isdigit()` it will always return `False`... – toom501 May 01 '18 at 12:44
0

From your code it is visible that you want your code to stop when the user inputs none and give output when the user inputs a digit.Until that you want to keep taking inputs. code:

from __future__ import print_function
import random
import time
repeat= True
none="none"
shutdowntime=3
repeatshut=True
def roll(count):
  print (">Rolls are these:")
  while count>0:
    print (random.randint(1,6))
    count-=1  
while repeat==True:
    while True:
        try:
            count=input("=>How many rolls do you want to roll?")
            x=int(count)
            roll(x)
        except Exception:
            if count==none:
                repeat=False
                break
            else:
                print("Enter valid choice")       

if repeat==False:
  print ("=>Thank you for using my dice roll.")
  while repeat==False and repeatshut==True:
    print ("%d seconds to shut down." % (shutdowntime))
    shutdowntime-=1
    time.sleep(1)
    if shutdowntime==0:
      repeatshut=False
      print ("Shutting down...")
      time.sleep(2)
      print ("Bye!")

I have used from future import print_function since i m using python 3 and from your code it seems you are using python 2.
So this code will work for you too :)
Do tell if it worked.

Aniket Bote
  • 3,456
  • 3
  • 15
  • 33
  • It did work thanks. But I did not understand why there is a brake in the line 26? – Elvez The Elf Apr 29 '18 at 13:39
  • Sorry i just edited everything above your break stmt.Forgot to remove it.This is new soln.If it did work for would you plz mark my ans accepted :) – Aniket Bote Apr 29 '18 at 13:42
  • No problem but is there a chance that we can turn an input (which will return a variable) into a part of a string? I'm going to use it if it can be done this way. – Elvez The Elf Apr 29 '18 at 13:45
  • like input is equal to a and no matter what a is I want to have a string "a" within it. – Elvez The Elf Apr 29 '18 at 13:45
  • input always gives you a string value even if your entered input is digit.If that is what you are asking. – Aniket Bote Apr 29 '18 at 13:47
  • Actually I asked it wrong it's not about the input, I mean I define a variable called maria. And in a string I want to have maria. This will be blahblahblah maria blahblahblah when printed. Like the % operator. – Elvez The Elf Apr 29 '18 at 13:51
  • so you mean a="maria" u want something like print("blalalala {} blalala".format(a)) so it will print "blalala maria blalala" – Aniket Bote Apr 29 '18 at 13:55
  • Uh yes, That is exactly it. – Elvez The Elf Apr 29 '18 at 15:22