How to take enter as input in python

How do i get the enter key to work in this situation? I tried searching for it, but maybe i'm wording it wrong. also, how would i get the else statement to work in this particular situation?

thank you

import random

def roll_dice():
    roll = random.randint(1,6)
    print("You rolled a %n " % roll)



def main():
    input("Hit ENTER to roll a single dice: ")
    roll_dice()
    else:
        print("exiting program.")

main()

asked Nov 29, 2017 at 19:07

How to take enter as input in python

0

You have to store input in a variable. Let it be enter.
User will hit enter and you will check if it was enter or not.
If input was an empty string then it is okay!

import random

def roll_dice():
    roll = random.randint(1,6)
    print("You rolled a %d " % roll)



def main():
    enter = input("Hit ENTER to roll a single dice: ")
    if enter == '':  # hitting enter == ''  empty string
        roll_dice()
    else:
        print("exiting program.")
        exit()

main()

answered Nov 29, 2017 at 19:15

Elis ByberiElis Byberi

1,3901 gold badge10 silver badges19 bronze badges

Just use:

if not input("Hit ENTER to roll a single dice: "):
    roll_dice()
else:
    print("exiting program.")

Also use a while loop instead, to ask user multiple times:

import random

def roll_dice():
    roll = random.randint(1,6)
    print("You rolled a {} ".format(roll))

def main():
    while True:
        if not input("Hit ENTER to roll a single dice: "):
            roll_dice()
        else:
            print("exiting program.")
            break

main()

If input() is non empty it will exit the program.

answered Nov 29, 2017 at 19:12

How to take enter as input in python

bhansabhansa

6,9062 gold badges28 silver badges50 bronze badges

2

In a case like this, would I usually do, is something like this:

if input == "":
    roll_dice()

I'm not sure if thats what you are looking for, though :3

answered Nov 29, 2017 at 19:09

How to take enter as input in python

0

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. While Python provides us with two inbuilt functions to read the input from the keyboard. 
     

    • input ( prompt )
    • raw_input ( prompt )

    input (): This function first takes the input from the user and converts it into a string. The type of the returned object always will be . It does not evaluate the expression it just returns the complete statement as String. For example, Python provides a built-in function called input which takes the input from the user. When the input function is called it stops the program and waits for the user’s input. When the user presses enter, the program resumes and returns what the user typed. 

    Syntax:

    inp = input('STATEMENT')
        
    Example:
    1.  >>> name = input('What is your name?\n')     # \n ---> newline  ---> It causes a line break
                >>> What is your name?
                Ram
                >>> print(name)
                Ram 
                
                # ---> comment in python

    Python3

    val = input("Enter your value: ")

    print(val)

    Output:

    How to take enter as input in python
     

    Taking String as an input:

    Python3

    name = input('What is your name?\n')    

    print(name)

    Output:

    What is your name?
    Ram
    Ram

    How the input function works in Python : 
     

    • When input() function executes program flow will be stopped until the user has given input.
    • The text or message displayed on the output screen to ask a user to enter an input value is optional i.e. the prompt, which will be printed on the screen is optional.
    • Whatever you enter as input, the input function converts it into a string. if you enter an integer value still input() function converts it into a string. You need to explicitly convert it into an integer in your code using typecasting. 

    How to take enter as input in python

    Code: 

    Python3

    num = input ("Enter number :")

    print(num)

    name1 = input("Enter name : ")

    print(name1)

    print ("type of number", type(num))

    print ("type of name", type(name1))

    Output: 

    How to take enter as input in python

    raw_input(): This function works in older version (like Python 2.x). This function takes exactly what is typed from the keyboard, converts it to string, and then returns it to the variable in which we want to store it.

    Example:

    Python

    g = raw_input("Enter your name : ")

    print g

    Output:

    How to take enter as input in python

     
    Here, g is a variable that will get the string value, typed by the user during the execution of the program. Typing of data for the raw_input() function is terminated by enter key. We can use raw_input() to enter numeric data also. In that case, we use typecasting. For more details on typecasting refer this. 

    Refer to the article Taking list as input from the user for more information. 


    How do you represent Enter key in Python?

    You should use \n as enter. It means the newline character.

    How do you take input until Enter is pressed in Python?

    To accept input until the Enter key is pressed:.
    Declare a variable that stores an empty list..
    Use a while loop to iterate an arbitrary number of times..
    Append each user input value to the list..
    Break out of the while loop when the user presses Enter..

    How can you accept input in Python?

    To receive information through the keyboard, Python uses the input() function. This function has an optional parameter, commonly known as prompt, which is a string that will be printed on the screen whenever the function is called.