Python 基于输入重新运行代码 打印(“==========================================================================================================”) 全球名称1 名称1=输入(“1).您的手机是什么品牌的?”) 条件=输入(“2).您的手机(新的或旧的)状况如何 摄像头=输入(“3).手机是否有摄像头(正确或错误)?) GPS=输入(“4).手机是否有GPS(正确或错误)?) WiFi=输入(“5).手机是否有WiFi(正确或错误)?) 全球价格1 价格1=输入(“6)。输入电话价格($0-$1000):) 如果(price1>“1000”或price1
,您需要将Python 基于输入重新运行代码 打印(“==========================================================================================================”) 全球名称1 名称1=输入(“1).您的手机是什么品牌的?”) 条件=输入(“2).您的手机(新的或旧的)状况如何 摄像头=输入(“3).手机是否有摄像头(正确或错误)?) GPS=输入(“4).手机是否有GPS(正确或错误)?) WiFi=输入(“5).手机是否有WiFi(正确或错误)?) 全球价格1 价格1=输入(“6)。输入电话价格($0-$1000):) 如果(price1>“1000”或price1,python,Python,,您需要将price1转换为整数–使用int()–而不是将其用作字符串,如果您想使用进行数字比较: print("=======================PHONE ONE INPUT=============================") global name1 name1=input("1). What brand is your phone? ") condition=input("2). What is the condition
price1
转换为整数–使用int()
–而不是将其用作字符串,如果您想使用
进行数字比较:
print("=======================PHONE ONE INPUT=============================")
global name1
name1=input("1). What brand is your phone? ")
condition=input("2). What is the condition of your phone (new or used)? ")
camera=input("3). Does the phone have a Camera (true or false)? ")
GPS=input("4). Does the phone have GPS (true or false)? ")
WiFi=input("5). Does the phone have WiFi (true or false)? ")
global price1
price1=input("6). Enter phone price($0-$1000) : ")
if (price1>"1000" or price1<"0"):
print("Invalid input, please re-run the simulation")
def main():
else:
computeValue(condition,GPS,WiFi,camera)
PhoneValue1=PhoneValue
Python 3\Python 2差异
在Python2中,input
将对键入的表达式求值并返回求值值,因此键入5
或2+3
将得到整数5
在Python3中,
input
与Python2中的raw\u input
相同,后者将给定的输入解释为字符串,因此5
将为您提供“5”
,2+3
将为您提供“5”
。这就是为什么我们需要使用int()
将输入转换为整数。请格式化您的代码,以便我们阅读。选择代码,然后单击输入框顶部的大括号。谢谢。当用户输入>1000或用户输入<0:…
时?如果我将其更改为while循环,如果值在0-1000之间,它将不会继续
price1=int(input("6). Enter phone price($0-$1000) : "))
if (price1> 1000 or price1 < 0):
print("Invalid input, please re-run the simulation")
main()
valid_price = False
while (not valid_price):
price1 = int(input("6). Enter phone price($0-$1000) : "))
if (price1> 1000 or price1 < 0):
valid_price = True
else:
print("Invalid input.")