Python: can't assign to literal

后端 未结 8 2226
旧时难觅i
旧时难觅i 2020-11-29 10:43

My task is to write a program that asks the user to enter 5 names which it stores in a list. Next, it picks one of these names at random and declares that person as the winn

相关标签:
8条回答
  • 2020-11-29 11:08

    This is taken from the Python docs:

    Identifiers (also referred to as names) are described by the following lexical definitions:
    
    identifier ::=  (letter|"_") (letter | digit | "_")*
    
    letter     ::=  lowercase | uppercase
    
    lowercase  ::=  "a"..."z"
    
    uppercase  ::=  "A"..."Z"
    
    digit      ::=  "0"..."9"
    
    Identifiers are unlimited in length. Case is significant.
    

    That should explain how to name your variables.

    0 讨论(0)
  • 2020-11-29 11:14

    Just adding 1 more scenario which may give the same error:

    If you try to assign values to multiple variables, then also you will receive same error. For e.g.

    In C (and many other languages), this is possible:

    int a=2, b=3;
    

    In Python:

    a=2, b=5
    

    will give error:

    can't assign to literal

    EDIT:

    As per Arne's comment below, you can do this in Python for single line assignments in a slightly different way: a, b = 2, 5

    0 讨论(0)
提交回复
热议问题