Python: Declare as integer and character

后端 未结 2 1588
无人及你
无人及你 2021-01-06 20:25
# declare score as integer
score = int

# declare rating as character
rating = chr

# write \"Enter score: \"
# input score
score = input(\"Enter score: \")

# if sc         


        
2条回答
  •  伪装坚强ぢ
    2021-01-06 21:21

    In python, you can't do static typing (i.e. you can't fix a variable to a type). Python is dynamic typing.

    What you need is to force a type to the input variable.

    # declare score as integer
    score = '0' # the default score
    
    # declare rating as character
    rating = 'D' # default rating
    
    # write "Enter score: "
    # input score
    score = input("Enter score: ")
    
    # here, we are going to force convert score to integer
    try:
        score = int (score)
    except:
        print ('score is not convertable to integer')
    
    # if score == 10 Then
    #   set rating = "A"
    # endif
    if score == 10:
        rating = "A"
    
    print(rating)
    

提交回复
热议问题