Reputation: 671
I am writing a simple program that takes user input as its arguments and runs these through a simple formula and then outputs the result. But I think something is wrong with my syntax and I don't understand what.
def P2(packets = input("Please enter nr of packets: "), switches = input("Please enter nr of switches: ")):
delay = 1
time = (packets + (switches-1)) * delay
#print("The time it takes to send " % packets % " packets back-to-back over " % switches % " switches is: " % time)
print("The time it takes to send () packets back-to-back over () switches is: ()".format(packets, switches, time))
P2(packets, switches)
I get error message:
Traceback (most recent call last):
File "C:/Users/amali/PycharmProjects/LearningPython/100PyEx1/P2Program.py", line 8, in <module>
P2(packets, switches)
NameError: name 'packets' is not defined
Upvotes: 0
Views: 58
Reputation: 11267
You need to define packets and switches outside your function and the call the function with them:
def P2(packets, switches):
delay = 1
time = (packets + (switches-1)) * delay
#print("The time it takes to send " % packets % " packets back-to-back over " % switches % " switches is: " % time)
print("The time it takes to send %s packets back-to-back over %s switches is: %s" %(packets, switches, time))
p = input("Please enter nr of packets: ")
s = input("Please enter nr of switches: ")
P2(p, s)
Edit:
If you don't need the actual variables, you can do:
P2(input("Please enter nr of packets: "), input("Please enter nr of switches: "))
I think that's less readable, but it works...
Upvotes: 3