如何解决Python为基数为10的int输入无效的文字
我的python代码运行正常。但是问题出在网站上,它希望输入为字符串格式,中间有空格,并且可以接受为整数
下面是我的实际python代码
import React from 'react'
import { Animated,Dimensions } from 'react-native'
import { PinchGestureHandler,State } from 'react-native-gesture-handler'
const Revue = () => {
scale = new Animated.Value(1)
onPinchEvent = Animated.event(
[
{
nativeEvent: { scale: this.scale }
}
],{
useNativeDriver: true
}
)
onPinchStateChange = event => {
if (event.nativeEvent.oldState === State.ACTIVE) {
Animated.spring(this.scale,{
toValue: 1,useNativeDriver: true
}).start()
}
}
return (
<PinchGestureHandler
onGestureEvent={this.onPinchEvent}
onHandlerStateChange={this.onPinchStateChange}>
<Animated.Image
source={require('../images/revue/01.jpg')}
style={{
width: screen.width,height: 300,transform: [{ scale: this.scale }]
}}
resizeMode='contain'
/>
</PinchGestureHandler>
)
}
export default Revue
此代码实际上计算出一个数字的幂。
例如: x = int(input())
n = int(input())
prod = 1
if(x >= 0 and x <= 9 and n >=0 and n <= 9):
for i in range(n):
prod = prod * x
print(prod)
现在的错误是
x = 3,n = 4,prod = 81
我必须采用 ValueError: invalid literal for int() with base 10: ' 3 4'
x = int(input())
n = int(input())
格式的两个输入x & n
。有人可以帮我吗?
解决方法
替换
x = int(input())
n = int(input())
使用
s = input('enter two integers: ')
x,n = [int(i) for i in s.split()]
,
无效,因为您的字符串包含空格。只需在空间上split
即可获得两个数字的列表,并可能还验证结果列表的长度,具体取决于您可能会得到什么作为输入。
在这里,您正在使用int()。因此,它需要在input()的引号内包含一个整数值。空间不属于整数类别。要解决此错误,请使用split()
,这是代码
x=int(input("Enter number1:"))
n=int(input("Enter number2:"))
prod = 1
if(x >= 0 and x <= 9 and n >=0 and n <= 9):
for i in range(n):
prod = prod * x
print(prod)
希望有帮助
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。