Reputation: 21
I wrote a little game in Python 3, and I want to repeat the dice rolling if the input is not 'stop'. If I write stop it is going to stop the program, so this is working, but when I write something else it is not repeating. Here is the source code:
print('Throw the dices, then type stop if you want to stop')
from random import *
while input != 'stop':
random1 = randint(1, 6)
random2 = randint(1, 6)
print('The numbers are: ', random1,' ', random2)
input = input()
Upvotes: 0
Views: 120
Reputation: 13757
I think there's probably a couple things that are confusing you (and other new Python users). This answer will attempt to clarify precisely why you're seeing the error you're seeing at the risk of sounding pedantic.
1) What does it mean to be callable?
Callables are objects that can be called by placing ()
at the end of said object (well sort of, some callables take arguments, but for now we'll pretend that they don't). For example, the builtin function input
is a callable. You can do input()
, for instance, like your program does. You can read more detail about the subject here: What is a "callable" in Python?
2) First class functions
Unlike other langauges you might be used to (like Java), Python has first class functions. This means that you can assign values to functions, and pass them around like objects. This is very powerful for programming. However, it also means that you can blast over a built-in function, like input
, and instead assign input
to a string. This is what your program does.
3) Strings can't be called
A string is a datatype which is not a callable. What would it mean to call or execute a string? So if you have x = "my_super_string"
, x()
doesn't make any sense. Thus, if you do this, the Python interpreter will give you a TypeError
saying that strings can't be called.
Here's a minimal example of what's happening in your code and why you get the TypeError
:
>>> input
<built-in function input>
>>> input = input()
"some string."
>>> input
'some string.'
>>> input()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
Note how the name input
changed from a <built-in function input>
to the string 'some_string.'
.
Upvotes: 1
Reputation: 22324
Your error arises from the fact you are overwriting input
. On the second iteration, input
is no longer a function but a string. Use some other name such as user_input
.
It is a common mistake to use names such as sum
, input
, max
for variables. Those should be avoided since they overwrite builtin functions.
Although, let me point out that iterating inputs until a given value is entered can be done with iter
second form.
from random import randint
print('Throw the dices, then type stop if you want to stop')
for _ in iter(input, 'stop'):
print('The numbers are: ', randint(1, 6), randint(1, 6))
As sidenotes, try to avoid using import *
and keep imports at the top of your script.
Upvotes: 2
Reputation: 334
You use both 'input' as variable name and as a function call. While the input-variable is being called first Python interprets it's like a str-type variable. Try changing that name to, ie. inp and see how it will behave.
Edit: you also have to define that inp as ie. "" before the loop starts.
Upvotes: 0