简体   繁体   English

ValueError: int() 以 10 为底的无效文字:'c'

[英]ValueError: invalid literal for int() with base 10: 'c'

i tried to make a project number guessing.我试图做一个项目编号猜测。 and i received an error:ValueError: invalid literal for int() with base 10: 'c'我收到了一个错误:ValueError: invalid literal for int() with base 10: 'c'

import random

print('i am think about a number between 1-20......\nwhould you try to discover?')
computer = random.randint(1,20)
for i in range(1,4):

    user = int(input("for a clue enter the letter 'c'\n enter a number between 1-20: "))
    if user == 'c':
        if int(computer) %2 == 0:
            print('this number is even number')
        else:
            print('this number is odd number')
    if user > computer :
        print('your choice is bigger than i thought\ntry again')
    elif user < computer:
        print('your choice is smaller than i thought\ntry again.')
    else:
        break
    
if user == computer:
    print('correct!!! you did it!!!')
else:
    print("better luck  next time the number i thought about is: " + str(computer))

You need to convert user variable to int after check if user == 'c :`.检查if user == 'c :` 后,您需要将user变量转换为int

import random

print('i am think about a number between 1-20......\nwhould you try to discover?')
computer = random.randint(1,20)
for i in range(1,4):

    user = input("for a clue enter the letter 'c'\n enter a number between 1-20: ")
    if user == 'c':
        if int(computer) %2 == 0:
            print('this number is even number')
        else:
            print('this number is odd number')
    else:
        user = int(user)
        if user > computer :
            print('your choice is bigger than i thought\ntry again')
        elif user < computer:
            print('your choice is smaller than i thought\ntry again.')
        else:
            break
    
if user == computer:
    print('correct!!! you did it!!!')
else:
    print("better luck  next time the number i thought about is: " + str(computer))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM